LoginSignup
1
1

More than 1 year has passed since last update.

How to create JSON from spreadsheet data

Posted at

Workflow

1.Create Google apps project.

2.Create files at project.
→env.gs&code.gs

3.Copy the code below
Put spreadsheet id & sheet name at the appropriate place.

env.gs
const SPREAD_SHEET = SpreadsheetApp.openById('spreadsheet id');
const SHEET_API =  "sheet name";
code.gs
function getData(sheetName) {
  var sheet = SPREAD_SHEET.getSheetByName(SHEET_API);
  const rows = sheet.getDataRange().getValues();
  const keys = rows.splice(0, 1)[0];
  return rows.map(row => {
    const obj = {};
    row.map((item, index) => {
      obj[String(keys[index])] = String(item);
    });
    return obj;
  });
}

function doGet() {
  var data = getData('SHEET_API');
  return ContentService.createTextOutput(JSON.stringify(data, null, 2))
  .setMimeType(ContentService.MimeType.JSON);
}

4.Push Deploy
Then select New Deploy
Change "Users who can access" to "Everyone"

5.Get url

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1