服务器功能针对特定电子表格将Null值返回给Apps Script Webapp [英] Null value returned to Apps Script webapp for a certain spreadsheet by server function
问题描述
我正在尝试将电子表格中的数据返回到以webApp形式发布的Apps脚本.但是,对于我的一个电子表格(链接),返回的数据为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. :)
推荐答案
我以前也遇到过同样的问题.当日期格式包含在data
中并由getSheetValues()
检索的值时,返回的值将为空.我认为当解析和/或转换日期值以及将值从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()
,进行如下修改.
For the function spreadsheetTest()
, modify as follows.
return data;
到 :
return JSON.stringify(data);
模式2
对于功能spreadsheetTest()
,进行如下修改.
Pattern 2
For the function spreadsheetTest()
, modify as follows.
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.
这篇关于服务器功能针对特定电子表格将Null值返回给Apps Script Webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!