Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save acurtis517/b51e00bfc94fa733bd0fafab70b1f336 to your computer and use it in GitHub Desktop.

Select an option

Save acurtis517/b51e00bfc94fa733bd0fafab70b1f336 to your computer and use it in GitHub Desktop.

Revisions

  1. @hayatbiralem hayatbiralem created this gist Jul 14, 2021.
    46 changes: 46 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // Thanks to this gist: [Sample HTML/JS to parse a Google Spreadsheet](https://gist.github.com/terrywbrady/a03b25fe42959b304b1e)

    var htmlEndPoint = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vREXBtuL2uKofq9ufXsIlytbONUR0-q_tf1Ucm14JpeE5KAdry97CCwvivf3e5NkCAnZ1Xg4qYa0RCo/pubhtml';

    (function($){

    var parseHtmlTableToJson = function($table){
    var data = [];
    var $headers = $("tr:first-child td", $table);
    var headers = [];
    $headers.each(function(){
    headers.push($(this).text().trim());
    });

    var $rows = $("tr:first-child ~ tr", $table).each(function(index) {
    data[index] = {};
    $(this).find("td").each(function(cellIndex) {
    data[index][headers[cellIndex]] = $(this).html();
    });
    });

    return data;
    };

    var parseGoogleSpreadsheetHTMLIntoArray = function (response){
    var sheets = {};

    var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(response)[1];
    console.log(bodyHtml);
    var $bodyHtml = $(bodyHtml);
    $bodyHtml.find('#sheet-menu a').each(function(index){
    sheets[$(this).text().trim()] = parseHtmlTableToJson($bodyHtml.find('table').eq(index));
    });

    return sheets;
    };

    $(document).ready(function(){
    $.ajax( htmlEndPoint )
    .done(function(response) {
    // console.log(response);
    var sheets = parseGoogleSpreadsheetHTMLIntoArray(response);
    console.log(sheets);
    });
    });
    })(jQuery);