繁体   English   中英

函数的Javascript(Google脚本)值未返回

[英]Javascript (Google Scripts) value from a function is not returning

我认为这可能是一个简单的解决方案,但我一直坚持解决这个简单的问题。

我已经调用了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;

};

即使我不是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的以下评论,即非强制性的分号...无论如何,代码在没有分号的情况下是有效的;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM