简体   繁体   中英

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

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()

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM