服务器功能为某个电子表格返回给 Apps Script webapp 的空值 [英] Null value returned to Apps Script webapp for a certain spreadsheet by server function
问题描述
我正在尝试将电子表格中的数据返回到作为 webApp 发布的 Apps 脚本.但是,对于我的其中一个电子表格(link),返回的数据为null
.我在返回之前记录了数据以确保它不为空,并且我可以在日志中看到它们.
I am trying to return the data in the spreadsheet to an Apps Script published as a webApp. However, for one of my spreadsheets (link), the returned data is null
. I logged the the data before returning to make sure that it isn't null, and I can see them in the logs.
这是我的代码:
function doGet(){
return HtmlService.createHtmlOutputFromFile('index');
}
function spreadsheetTest() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var maxRows = sheet.getMaxRows();
var data = sheet.getSheetValues(1, 1, maxRows, 10);
Logger.log(data)
return data;
}
<html>
<button type="button" onclick="clcked();">Click Me!</button>
<div id="Message"></div>
<script>
function clcked(){
google.script.run
.withSuccessHandler(function(response) {
console.log(response);
})
.spreadsheetTest();
}
</script>
</html>
任何帮助将不胜感激.:)
Any help would be highly appreciated. :)
推荐答案
我以前也遇到过同样的问题.当getSheetValues()
检索到的data
中包含日期格式的值时,返回值为空.我认为当日期值被解析和/或转换时,当值从 GAS 发送到 Javascript 时,可能会出现问题.这样,返回 null.为了避免这种情况,我认为您的情况有两种模式.请根据您的情况选择其中之一.
I have ever experienced the same issue before. When the values with the date format are included in data
which is retrieved by getSheetValues()
, the returned values become null. I thought that the issue might occur when the date values are parsed and/or converted, when the values are sent from GAS to Javascript. By this, null is returned. In order to avoid this, I think that there are 2 patterns for your situation. Please chose one of them for your situation.
对于函数spreadsheetTest()
,修改如下.
return data;
到 :
return JSON.stringify(data);
模式 2
对于函数spreadsheetTest()
,修改如下.
var data = sheet.getSheetValues(1, 1, maxRows, 10);
到 :
var data = sheet.getRange(1, 1, maxRows, 10).getDisplayValues();
如果这些对您的情况没有帮助,我很抱歉.
If these are not useful for your situation, I'm sorry.
这篇关于服务器功能为某个电子表格返回给 Apps Script webapp 的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!