首页 > 解决方案 > 如何在 google app 脚本中获取我的 google sheet 的 json 格式?

问题描述

我正在尝试获取我的谷歌表格的 json 格式。我尝试了很多教程,但它不起作用。

标签: javascriptjsongoogle-apps-scriptgoogle-sheetsgoogle-sheets-api

解决方案


这个怎么样:

function shjsn() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('SheetName');
  const vs=sh.getDataRange().getValues();
  return JSON.stringify(vs);
}

或者也许对于整个电子表格:

function ssjsn() {
  const ss=SpreadsheetApp.getActive();
  const shts=ss.getSheets();
  let vs=[];
  shts.forEach(function(sh,i){vs.push(sh.getDataRange().getValues());});
  return JSON.stringify(vs);
}

推荐阅读