我对我的 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/