Retrieve Google Spreadsheet Content as a List of Dictionaries using GAS

2022-10-25 09:04 (2 years ago) ytyng

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

Comments

Archive

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011