Google Apps脚本返回数组值,并在javascript函数中使用它们

用户名

我正在尝试返回一个数组,并在javascript函数中使用它,但它似乎不起作用。我的Code.gs如下:

function doGet() {
  return HtmlService.createHtmlOutputFromFile('test')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function test() {
    var locations = [];
    var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/13q7pIeMUHll6_5xBUpBavaBqALt9fnFnOIO-Hwy_pFc/edit'),
    sheet = ss.getActiveSheet(),
    range = ss.getRange("D2:D4"),
    values = range.getValues();
    for (var r=1; r<values.length; r++) {
       var row = values[r];
       locations.push(row[0]);
    }
    return locations;
}

我的test.html中的函数如下所示:

function hello() {
   google.script.run.test();
}

所以我想将数组及其内容传递给test.html中的hello函数。我该如何进行这项工作?

艾伦·威尔斯(Alan Wells)

您需要一个withSuccessHandler()链接到您方法google.script.run

function hello() {
  google.script.run
    .withSuccessHandler(injectHTML)
    .test();
}

//This captures the returned string from the server side code
function injectHTML(argReturnedArray) {
  //To Do - code to inject the HTML

};

不幸的是,服务器端.gs代码只会返回一个字符串。但是有一种方法可以解决这个问题。采用:

JSON.stringify(yourArray);

您的数组名为locations

return JSON.stringify(locations);

现在,您需要将JSON字符串转换回数组:

function injectHTML(argReturnedArray) {
  /* Put the array into the browsers window object in order to make the
  *  array named myReturnedArray available to all other functions.
  */
  window.myReturnedArray = JSON.parse(argReturnedArray);

  //To Do - code to inject the HTML
};

//Get the array in another function
function myOtherFunction() {
  //Get the array from the browsers window object
  var theArray = window.myReturnedArray;
  var i=0, thisElement="";
  for (i=0;i<theArray.length;i+=1) {
    thisElement = theArray[i];
  }
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用Rails安全地存储Google Api密钥并在脚本中使用它们?

来自分类Dev

在 Google 脚本函数中读取单元格并在函数 #inception 中使用它

来自分类Dev

从函数{google apps脚本}获取返回值

来自分类Dev

在HTMLService / Google Apps脚本中使用日期选择器返回值

来自分类Dev

在HTMLService / Google Apps脚本中使用日期选择器返回值

来自分类Dev

在Google Apps脚本中使用Google字体

来自分类Dev

在Google Apps脚本中使用If / Then函数进行日期验证

来自分类Dev

我可以在Google表格中创建一个函数并从网络应用程序Google Apps脚本中使用它吗?

来自分类Dev

在Google Apps脚本中返回数组

来自分类Dev

在Google Apps脚本中返回空值

来自分类Dev

使用Google Apps脚本,在javascript中使用“ .hasOwnProperty(i)”将数组中的每个字符识别为true

来自分类Dev

无法在Google Apps脚本中使用fetchURL()

来自分类Dev

在Google Apps脚本中使用slices setoption

来自分类Dev

Google Apps脚本:在公式中使用Var?

来自分类Dev

Google Apps Script - 将应用程序脚本函数的输出返回到 html 文件 javascript 函数

来自分类Dev

在Google Apps脚本中使用google docs api

来自分类Dev

Google Apps脚本函数的返回值未显示在电子表格中

来自分类Dev

Google Apps脚本:如何从复选框列表中的提交函数访问数组值?

来自分类常见问题

返回匹配数组的索引不起作用-Javascript Google Apps脚本

来自分类Dev

doGet函数中的Google Apps脚本返回图像

来自分类Dev

Apps脚本-无法在html模板的js函数中使用google.script.run吗?

来自分类Dev

如何在Google Apps脚本中使用嵌套数组解析JSON对象

来自分类Dev

Google Apps脚本错误值

来自分类Dev

可以在Google App脚本中使用Google Spreadsheet函数吗?

来自分类Dev

可以在Google App脚本中使用Google Spreadsheet函数吗?

来自分类Dev

函数的Javascript(Google Scripts)值未返回

来自分类Dev

删除数组Google Apps脚本中的重复值

来自分类Dev

删除数组Google Apps脚本中的重复值

来自分类Dev

Google Apps脚本在数组中查找值

Related 相关文章

  1. 1

    如何使用Rails安全地存储Google Api密钥并在脚本中使用它们?

  2. 2

    在 Google 脚本函数中读取单元格并在函数 #inception 中使用它

  3. 3

    从函数{google apps脚本}获取返回值

  4. 4

    在HTMLService / Google Apps脚本中使用日期选择器返回值

  5. 5

    在HTMLService / Google Apps脚本中使用日期选择器返回值

  6. 6

    在Google Apps脚本中使用Google字体

  7. 7

    在Google Apps脚本中使用If / Then函数进行日期验证

  8. 8

    我可以在Google表格中创建一个函数并从网络应用程序Google Apps脚本中使用它吗?

  9. 9

    在Google Apps脚本中返回数组

  10. 10

    在Google Apps脚本中返回空值

  11. 11

    使用Google Apps脚本,在javascript中使用“ .hasOwnProperty(i)”将数组中的每个字符识别为true

  12. 12

    无法在Google Apps脚本中使用fetchURL()

  13. 13

    在Google Apps脚本中使用slices setoption

  14. 14

    Google Apps脚本:在公式中使用Var?

  15. 15

    Google Apps Script - 将应用程序脚本函数的输出返回到 html 文件 javascript 函数

  16. 16

    在Google Apps脚本中使用google docs api

  17. 17

    Google Apps脚本函数的返回值未显示在电子表格中

  18. 18

    Google Apps脚本:如何从复选框列表中的提交函数访问数组值?

  19. 19

    返回匹配数组的索引不起作用-Javascript Google Apps脚本

  20. 20

    doGet函数中的Google Apps脚本返回图像

  21. 21

    Apps脚本-无法在html模板的js函数中使用google.script.run吗?

  22. 22

    如何在Google Apps脚本中使用嵌套数组解析JSON对象

  23. 23

    Google Apps脚本错误值

  24. 24

    可以在Google App脚本中使用Google Spreadsheet函数吗?

  25. 25

    可以在Google App脚本中使用Google Spreadsheet函数吗?

  26. 26

    函数的Javascript(Google Scripts)值未返回

  27. 27

    删除数组Google Apps脚本中的重复值

  28. 28

    删除数组Google Apps脚本中的重复值

  29. 29

    Google Apps脚本在数组中查找值

热门标签

归档