Home > Enterprise >  extract data from all sheets in a spreadsheet with googe apps script
extract data from all sheets in a spreadsheet with googe apps script

Time:08-10

I am trying to extract all the data from different sheets that I have in a spreadsheet, but I am facing errors while trying to do it, I am not very familiar with JS if anyone out there can help me out.

function getData(s){

    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const ws = ss.getSheetByName(s);
    const data = ws.getRange("A1").getDataRegion().getValues();
    const headers = data.shift();
    const jsonArray = data.map(r =>{
      let obj = {};
      headers.forEach((h, i) => {
        obj[h] = r[i]
      });
      return obj
  })
  const res = {'sheets': jsonArray} 
  }

function getDo(){

  var sheetsN = ['check1', 'check2', 'check3', 'check4', 'check5']

  for ( var s=0; s<sheetsN.length; s  ) {
    dataAll = getData(console.log(sheetsN[s]))
  }
  return ContentService.createTextOutput(JSON.stringify(dataAll))
  .setMimeType(ContentService.MimeType.JSON)
}

getDo()

CodePudding user response:

Get all data in all sheets in a spreadsheet

function getAllData() {
  const ss = SpreadsheetApp.getActive();
  let obj = {}
  ss.getSheets().forEach(sh => {
    obj[sh.getName()] = sh.getDataRange().getValues();
  })
  //Logger.log(JSON.stringify(obj));
  return obj;
}
  • Related