来自函数的Javascript(Google Scripts)值未返回

时间:2015-06-19 17:39:12

标签: javascript google-apps-script google-sheets google-calendar-api

我认为这可能是一个简单的解决方法,我无法解决这个简单的问题。

我调用了一个createEvent函数来创建一个google日历事件。 作为此功能的一部分,我还将Google日历事件ID设为EventId,并希望将其返回。

由于某些原因我不太明白,EventId值不会返回。

  var EventId;

  createEvent(calendarId,title,startDt,endDt,desc,EventId);

  Logger.log('id after func = '+EventId);

   sheet.getRange(lr,EventIdHolder,1,1).setValue(EventId);
};

function createEvent(calendarId,title,startDt,endDt,desc,EventId) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var start = new Date(startDt);
  var end = new Date(endDt);
//Manually set the Location, this can be modified to be dynamic by modifying the code if need be
  //var loc = sheet.getRange(lr,destId,1,1).getValue();
  var loc = "Some Location"
//Set the Options, in this case we are only using Description and Location, as we do not need Guests or sendInvites
  var event = cal.createEvent(title, start, end, {
      description : desc,
      location : loc
  });
   Logger.log('event = '+event);

   var EventId = event.getId();

   Logger.log('id generated = '+EventId);

   return EventId;

};

1 个答案:

答案 0 :(得分:1)

即使我不是JavaScript专家,我也觉得这样写它更合乎逻辑(直接为EventId变量赋值):

  ...
  var EventId = createEvent(calendarId,title,startDt,endDt,desc);
  Logger.log('id after func = '+EventId);
  sheet.getRange(lr,EventIdHolder,1,1).setValue(EventId);
}

function createEvent(calendarId,title,startDt,endDt,desc) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var start = new Date(startDt);
  var end = new Date(endDt);
  //Manually set the Location, this can be modified to be dynamic by modifying the code if need be
  //var loc = sheet.getRange(lr,destId,1,1).getValue();
  var loc = "Some Location"
  //Set the Options, in this case we are only using Description and Location, as we do not need Guests or sendInvites
  var event = cal.createEvent(title, start, end, {
    description : desc,
    location : loc
  });
  Logger.log('event = '+event);
  var EventId = event.getId();
  Logger.log('id generated = '+EventId);
  return EventId; 
}

注意:我同意Mogsdad的评论,关于半数列不是强制性的......无论如何,代码在没有它的情况下工作; - )