Code.gs中的Google Apps脚本返回对象 [英] Google Apps Script return object in Code.gs
问题描述
是否无法从服务器端返回Google Apps Script中的对象? 在我的HTML(叫做Agenda.html)中,我有这个: 这应该只是返回从数组中的事件数据定义的对象。这不起作用,但是,我收到一个错误,脚本已成功完成,但返回的值不是有效的返回类型。如果我将return语句更改为 我的Code.gs doGet有这个: 我自己发现它,显然Date-types不是合法的参数类型或返回类型(这也适用于数组,对象等变量)。我已经将每个Date作为参数或返回类型更改为.toString(),现在它可以工作。有了这些日期字符串,我也可以为这些日期创建日期对象。 Is it not possible to return an object in Google Apps Script from server-side?
My code is as follows: In my HTML (called Agenda.html) I have this: This should just return the objects I have defined from the events data in an array. This does not work however, I'm getting an error that the script has completed succesfully, but the returned value is not a valid return type. If I change the return statement to My Code.gs doGet has this:
I found it myself, apparently Date-types are not a legal parameter-type or return-type (this also applies to variables in arrays, objects, ...). I have changed every Date that I use as parameter or return-type to .toString(), and now it works. With these date-strings I can create Date-objects for those dates as well. The 这篇关于Code.gs中的Google Apps脚本返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
我的代码如下:
$ b $ pre $ function getAllEventsOfDate(){
var cal = CalendarApp.getCalendarById( 。Session.getActiveUser()getEmail());
var events = cal.getEventsForDay(new Date());
var results = [];
for(var i = 0; i< events.length; i ++){
var event = {title:events [i] .getTitle(),desc:events [i] .getDescription() ,location:events [i] .getLocation(),startTime:events [i] .getStartTime(),endTime:events [i] .getEndTime()};
results.push(event);
}
Logger.log(results);
返回结果;
$ b $ p
$ b
$(document).ready(function(){
google.script.run.withSuccessHandler(getAgendaData).getAllEventsOfDate() ;
});
函数getAgendaData(数据)
{
console.log(data);
$ b $ p
$ b return results [0];
,它也不起作用(想要检查它是否是由数组引起的)。如果我将它更改为返回结果[0] .title;
它确实有效:我的日历的第一个事件的标题显示在控制台中。
这是否意味着我无法返回对象?任何帮助将不胜感激。
function doGet(){
return HtmlService.createTemplateFromFile('Agenda')。evaluate();
}
更新
.gs
脚本可以返回一个对象。从您的更新中看起来您发现问题。function getAllEventsOfDate(){
var cal = CalendarApp.getCalendarById(Session.getActiveUser().getEmail());
var events = cal.getEventsForDay(new Date());
var results = [];
for(var i = 0; i < events.length;i++){
var event = {title:events[i].getTitle(), desc:events[i].getDescription(), location:events[i].getLocation(), startTime:events[i].getStartTime(), endTime: events[i].getEndTime()};
results.push(event);
}
Logger.log(results);
return results;
}
$(document).ready(function(){
google.script.run.withSuccessHandler(getAgendaData).getAllEventsOfDate();
});
function getAgendaData(data)
{
console.log(data);
}
return results[0];
, it doesn't work either (wanted to check if it was caused by the array). If I change it to return results[0].title;
it does work: the title of the first event of my calendar is displayed in the console.
Does this mean I can't return objects? Any help would be much appreciated.function doGet(){
return HtmlService.createTemplateFromFile('Agenda').evaluate();
}
Update
.gs
script can return an object. From your update it looks like you found the problem.