Code.gs中的Google Apps脚本返回对象 [英] Google Apps Script return object in Code.gs

查看:117
本文介绍了Code.gs中的Google Apps脚本返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否无法从服务器端返回Google Apps Script中的对象?
我的代码如下:
$ 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

在我的HTML(叫做Agenda.html)中,我有这个:

  $(document).ready(function(){
google.script.run.withSuccessHandler(getAgendaData).getAllEventsOfDate() ;
});

函数getAgendaData(数据)
{
console.log(data);



$ b $ p
$ b

这应该只是返回从数组中的事件数据定义的对象。这不起作用,但是,我收到一个错误,脚本已成功完成,但返回的值不是有效的返回类型。如果我将return语句更改为 return results [0]; ,它也不起作用(想要检查它是否是由数组引起的)。如果我将它更改为返回结果[0] .title; 它确实有效:我的日历的第一个事件的标题显示在控制台中。
这是否意味着我无法返回对象?任何帮助将不胜感激。



我的Code.gs doGet有这个:

  function doGet(){
return HtmlService.createTemplateFromFile('Agenda')。evaluate();
}



更新



我自己发现它,显然Date-types不是合法的参数类型或返回类型(这也适用于数组,对象等变量)。我已经将每个Date作为参数或返回类型更改为.toString(),现在它可以工作。有了这些日期字符串,我也可以为这些日期创建日期对象。

解决方案

.gs 脚本可以返回一个对象。从您的更新中看起来您发现问题。


Is it not possible to return an object in Google Apps Script from server-side? My code is as follows:

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;
}

In my HTML (called Agenda.html) I have this:

$(document).ready(function(){
  google.script.run.withSuccessHandler(getAgendaData).getAllEventsOfDate();
 });

function getAgendaData(data)
{
    console.log(data);
}

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 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.

My Code.gs doGet has this:

function doGet(){
    return HtmlService.createTemplateFromFile('Agenda').evaluate();
}

Update

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 .gs script can return an object. From your update it looks like you found the problem.

这篇关于Code.gs中的Google Apps脚本返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆