Retrieve Google Spreadsheet Content as a List of Dictionaries using GAS

2022-10-25 00:04 (3 years ago)
Retrieve Google Spreadsheet Content as a List of Dictionaries using GAS

Certainly! Here is the translation of the provided Japanese blog article into English:


function myFunction() {
  var data = getDataOfSheetId('xxxxx-xxxxxxxxxxxxxxxxxxxx_xxxxxx', 0);
  console.log(data);
}

function getDataOfSheetId(spreadsheetId, sheetIndex) {
  var book = SpreadsheetApp.openById(spreadsheetId);
  var sheet = book.getSheets()[sheetIndex];
  console.log(sheet.getName());
  var [rows, columns] = [sheet.getLastRow(), sheet.getLastColumn()];
  var data = sheet.getRange(1, 1, rows, columns).getValues();
  var header = data[0];
  data.shift();
  return data.map(function(row) {
    return header.reduce(function(o, h, i) {
      o[h] = row[i];
      return o;
    }, {});
  });
}

Reference:

javascript - How to get data from google sheets as an array of dictionary - Stack Overflow


Feel free to let me know if you need any further assistance!

Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive