简体   繁体   English

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

[英]Google Apps Script is changing my Array into a string. Why?

I have a fairly simple call to my code.gs file for some data that lives in a sheet. 我对code.gs文件进行了相当简单的调用,以获取生活在工作表中的一些数据。 I am calling this function from a html file to populate a drop down list. 我从一个html文件调用此函数来填充下拉列表。 So: 所以:

Code.gs Code.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 的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>

When my result hits the html page it is no longer an array of arrays, rather a single string of comma separated values. 当我的结果到达html页面时,它不再是一个数组数组,而是一个逗号分隔值的字符串。

Can someone please explain what is happening here, and how to fix? 有人可以解释一下这里发生了什么,以及如何解决? Thank you. 谢谢。

here is a fix 这是一个修复
on the client you need to write: 在客户端上你需要写:

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

on the server: 在服务器上:

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

When using the HTML Service: Templated HTML , the code you write is not executed directly, the server evaluates the template and returns an HtmlOutput object , that the script can serve to the user. 使用HTML服务:模板化HTML时 ,您编写的代码不会直接执行,服务器会评估模板并返回一个HtmlOutput对象 ,该脚本可以为用户提供。

Printing scriptlets execute code and append new content to the HtmlOutput using contextual escaping to avoid accidentally allowing a cross site scripting (XSS) bug , so the result is a safe String with no markup that can cause unexpected code execution. 打印scriptlet执行代码并使用上下文转义将新内容附加到HtmlOutput,以避免意外地允许跨站点脚本(XSS)错误 ,因此结果是一个安全的String,没有可能导致意外代码执行的标记。

You can: 您可以:

index.html 的index.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
// The code in this function runs when the page is loaded.
$(function() {
  google.script.run.withSuccessHandler(populateStuff)
      .getTheStuffINeed();
});

function populateStuff(stuff) {
  var options = '';
  for (var i = 0; i < stuff.length; i++){
    options += '<option id="'+stuff[i][0]+'" value="'+ stuff[i][1] + '">' + stuff[i][2] + '</option>';
  }
  $('#select').append(options);
}
</script>
  • Create and populate the select in the template ( NOT following the best practices ). 创建并填充模板中的选择( 遵循最佳实践 )。

index.html 的index.html

<select>
  <?
  var myStuff= getTheStuffINeed(); 
    for (var i = 0; i < myStuff.length; i++) { ?>
      <option id="<?= myStuff[i][0] ?>" value="<?= myStuff[i][1] ?>"><?= myStuff[i][2] ?></option>
  <? } ?>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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