无法将值传递给HtmlService

时间:2015-10-21 21:41:04

标签: google-apps-script

以下是我用来获取电子表格中所有有效值的函数:

function getAllValuesInSheet(){
  var values = SpreadsheetApp.getActiveSpreadsheet().getDataRange().getValues();
  Logger.log(values);
  return values;
}

这将返回我的值的2D数组。

在我的HtmlService中,我有:

<script>

    function onSuccess(values) {
      console.log("ran");
      console.log(values);
     }

    function populate(){
      google.script.run.withSuccessHandler(onSuccess).getAllValuesInSheet();
    }


</script>

<script>
  // Using the "load" event to execute the function "populate"
  window.addEventListener('load', populate);
</script>

我收到错误Uncaught ScriptError: The script completed but the returned value is not a supported return type.

我的功能类似于这个功能非常好。我想知道数据是否太大而无法通过?它不是那么大,不到500个细胞。

为什么我收到此错误?我怎样才能解决这个问题?我正在尝试将所有电子表格数据加载到我的HtmlService。这对小型数据集来说是不好的做法吗?我应该来回进行ajax调用吗?

2 个答案:

答案 0 :(得分:1)

尝试使用函数JSON.stringify(object)将数组转换为json格式,以便更容易处理数组。

你的代码就像:

return JSON.stringify(values);

检查这对您是否有用。

答案 1 :(得分:0)

Gerardo的答案之所以有效是因为它确保您的服务器端函数返回一个String,这是一个JavaScript原语。

如果任何数组内容由不支持的类型(如Date对象)组成,则原始请求将失败。

来源:HTML Service: Communicate with Server Functions