OpenEugene / OpenEugeneFestivalWeb

The Web site for the Open Eugene Festival
MIT License
3 stars 2 forks source link

Google Sheets Script Endpoint #47

Open mckelveygreg opened 5 years ago

mckelveygreg commented 5 years ago

You can attach a "google script" to a google sheet to turn your info into a REST API. All that needs to happen is the creator of the sheet needs to attach the script (Tools > Script Editor), and then publish the script (Publish > Deploy as Web App). Gatsby would then use that url to fetch the json. Therefore, on each build, Gatsby would grab the latest info from the sheet.

This replaces exporting json from sheets, adding that file to the repo, and then doing another site build.

Needs:

mckelveygreg commented 5 years ago

Code.gs

/**
 * @OnlyCurrentDoc
 */

// Function that is exposed
function doGet(e) {
    var output = getOutputObjectFromSheet(e.parameter);
    return ContentService.createTextOutput(JSON.stringify(output));
}

function getOutputObjectFromSheet(params) {
  // Inspired by: http://ramblings.mcpher.com/Home/excelquirks/gassnips/namedcolumns
  // get all the data on the sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  var values = sheet.getDataRange().getValues();

  // separete the headeres and data
  var headers = values[0];
  var data = values.slice(1);

  // Build result as an array of objects
  var result = []
  data.forEach(function(d,i) {
    var object = {};
    headers.forEach(function(h, j) {
      object[h] = d[j];
    });
    result.push(object);
  });

  return result;
}
markdav-is commented 5 years ago

image

Open Eugene Conference Leadership Team Roster API endpoint: https://script.google.com/macros/s/AKfycbzMa6BvpG0lMFSmL87L4nCPLk5j7BaZnJ9-Is7y03LeefWiKyo/exec

markdav-is commented 5 years ago

Open Eugene Call for Projects https://script.google.com/macros/s/AKfycbzfzMqhgN2vFovjEJXNIZjKqrINiwVoeoAol7xT5S058DS5TBXi/exec

markdav-is commented 5 years ago

Open Eugene Call for Programming https://script.google.com/macros/s/AKfycbyVcGpheIX9oOpHc3SWtiJn-koFIk1X-MiFwm6NRsPgRpaCJ88k/exec

markdav-is commented 5 years ago

OEF Schedule https://script.google.com/macros/s/AKfycbw4vA3qTPxjpHLPAtw4HIhpB6-OYdbDjpnQFtdnbB4SmJxh5YE/exec

markdav-is commented 5 years ago

Media Links https://script.google.com/macros/s/AKfycbyYU503ELCLjGToUDRogOZ1Kxanl4coR6sUnN_i1FmAHocGg8CE/exec

markdav-is commented 5 years ago

Sponsor Web Logos and Links https://script.google.com/macros/s/AKfycbyytX06ACX5fV8inFiIDtVgyj_o6Vhu3zEEi1h5XRVFknoXUHRe/exec

markdav-is commented 5 years ago

@mckelveygreg I think that's it!

markdav-is commented 5 years ago

actually none of these work. the code has errors :(

mckelveygreg commented 5 years ago

lolz... found a little bug! Fixed it, and updated the above script. it actually should work now, though!