javascript - Google Apps 脚本正在将我的数组更改为字符串。为什么?

标签 javascript google-apps-script google-sheets

我对我的 code.gs 文件进行了相当简单的调用,以获取工作表中的一些数据。我从一个 html 文件调用这个函数来填充一个下拉列表。所以:

代码.gs

function getTheStuffINeed() {

  // Get data from sheet
  var ss =  SpreadsheetApp.openById("ss_id");
  var sheet = ss.getSheetByName("sheetname");
  var myStuff = sheet.getRange(2, 1, sheet.getLastRow()-1, 3).getValues();

  Logger.log(Array.isArray(myStuff));  // true. 
  Logger.log(myStuff); // [[id1, aVal, aSecondVal], [id2, AnotherVal, anotherSecondVal]] 

  return myStuff ; // An array of arrays
}

index.html

<script>
  // get data
  var myStuff= <?= getTheStuffINeed() ?>;  

  console.log(Array.isArray(myStuff)); // false
  console.log(myStuff); // id1, aVal, aSecondVal, id2, AnotherVal, anotherSecondVal, etc
</script>

当我的结果到达 html 页面时,它不再是数组的数组,而是逗号分隔值的单个字符串。

有人可以解释一下这里发生了什么,以及如何解决吗?谢谢。

最佳答案

这里有一个修复
在客户端你需要写:

var myStuff= JSON.parse(<?= getTheStuffINeed() ?>); 

在服务器上:

  return JSON.stringify(myStuff) ; // An array of arrays

关于javascript - Google Apps 脚本正在将我的数组更改为字符串。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38973103/

相关文章:

google-sheets - 谷歌表格: Whats the formula to count all Sundays from a list of dates

google-apps-script - 如何在谷歌表格中生成uuid?

javascript - 获取 javascript 值到 C# 字符串

javascript - Backbonejs - 我把计算放在哪里?

javascript - 我的 GAS 脚本无法创建没有任何错误的事件

javascript - 触发以获取 google-apps-script 中的 "Exceeded maximum execution time"附近

javascript - 在 XSLT 中导入 JS 文件和 Javascript

javascript - js 代码在开发人员工具中运行良好,但在 js 文件中运行不正常

google-apps-script - 通过 Google Apps 脚本发布 Google 电子表格

html - 如何在 Google App Script 的 HTML 服务中插入完全适合的背景图片?