【问题标题】:Google Script - Running script by trigger changes date formatGoogle Script - 通过触发器更改日期格式运行脚本
【发布时间】:2021-02-09 17:57:27
【问题描述】:

我是新来的,对 Google Scripts 的 js 有点陌生。

我创建了一个表单,它填充了一个工作表,并希望使用工作表中的数据更新日历。此表单和日历将与一大群人共享,因此我们都可以创建特定格式的事件,并避免删除或修改不应修改的事件。

当我从工作区运行脚本时,它会正常运行,但在定义运行它的触发器后,日期格式会发生变化(我相信这是原因,但我不确定)并且所有事件都是为 1969 年创建的. 下面我介绍部分脚本。

因此,当我从工作空间启动脚本时,它再次运行良好,但在通过触发器 (onsubmit) 运行时它会更改日期格式。
我已经删除并重新创建了触发器,但它不起作用。
日期格式设置为 dd/MM/yyyy,时间设置为 HH:mm:ss。

有人知道如何解决这个问题吗?
提前感谢
最好的问候

function create_event_PRH(){\
  var calId = ID from Calendar;\
  var calendar = CalendarApp.getCalendarById(calId);\
  var timezone=calendar.getTimeZone();


  // Getting data from the spreadsheet\
  var ss = SpreadsheetApp.getActive();\
  var ss_name = ss.getName();\
  var sheet = ss.getActiveSheet();\
  var range = sheet.getDataRange();\
  var values = range.getValues();\
  var lastRow = sheet.getLastRow()-1;

  // Definition of the starting and finishing time in the spreadsheet (I am using this part of the script to sum date and time of the day)\
  var range = sheet.getRange('P'+String(lastRow+1)).setFormula('H'+String(lastRow+1)+'+I'+String(lastRow+1));\
  var range = sheet.getRange('Q'+String(lastRow+1)).setFormula('H'+String(lastRow+1)+'+J'+String(lastRow+1));\
  Logger.log(Utilities.formatDate(new Date(), timezone, 'dd/MM/yyyy HH:mm:ss'));

  // Start and end time
  var event_values = values[lastRow];\
  var event_start_time=new Date(event_values[15]);\
  var event_end_time=new Date(event_values[16]);


  // Event description and creation\
  var lecture_title=event_values[5];\
  var program_number=event_values[2];\
  var event_title = 'Program'+program_number+' - '+lecture_title;\
  var description='Presenter: '+event_values[10]+'\nInstitution:'+event_values[11];\
  var event = calendar.createEvent(event_title,event_start_time,event_end_time);\
  event = event.setDescription(description);\
}

【问题讨论】:

  • new Date(event_values[15])中,event_values[15]的值是多少?见Why does Date.parse give incorrect results?
  • 你好!为了更好地分析脚本,你能分享一下事件的价值日志吗?这将包括在事件创建发生之前的event_titleevent_start_timeevent_end_time 变量的值。您是否可以使用Try this API! 复制此问题?如果是这样,请分享电话和回复。
  • 亲爱的 RobG,event_values[15] 和 [16] 是 dd/MM/yyyy HH:mm:ss 格式的开始和结束时间,它作为字符串从单元格中传递。
  • 亲爱的@Jacques-GuzelHeron\ event_title 的日志数据正常。但是,触发器运行时与开始和结束时间相关的日志很奇怪,如下:\ Event Start time from event.getStartime: Wed Dec 31 1969 19:00:00 GMT-0500 (Eastern Standard Time)\ Event End事件开始时间:1969 年 12 月 31 日星期三 19:00:00 GMT-0500(东部标准时间)\ 来自变量的事件开始时间:无效日期\ 来自变量的事件结束时间:无效日期\单元格值为 05/02/ 2021 12:00:00 和 05/02/2021 13:00:00\
  • 从工作区运行,日志如下: Event Start time from event.getStartime: Fri Feb 05 2021 10:00:00 GMT-0500 (Eastern Standard Time)\ Event End time from事件:2021 年 2 月 5 日星期五 11:00:00 GMT-0500(东部标准时间)\ 变量的事件开始时间:2021 年 2 月 5 日星期五 10:00:00 GMT-0500(东部标准时间)\ 事件结束时间来自变量:2021 年 2 月 5 日星期五 11:00:00 GMT-0500(东部标准时间)

标签: javascript date google-apps-script


【解决方案1】:

特别是@Jacques-GuzelHeron 和@RobG,
问题与我使用电子表格从表单中组合(添加)日期(dd/MM/yyyy)和时间(HH:mm:ss)并检索它以获取日期对象的事实有关。但我不知道电子表格在运行时没有更新,脚本只使用缓存数据...
所以,我不得不手动组合这两个参数。我还没有找到更好的方法来做到这一点,但下面的代码工作正常。

  Logger.log(Utilities.formatDate(new Date(), 'America/Sao_Paulo', 'dd/MM/yyyy HH:mm:ss'));

  // Start and end time (the procedure below was needed to combine date and time)
  var event_values = values[lastRow];
  // The commands in the next lines were required to combine date and time
  // event_values[7] corresponds to the date of the event provided by the form to the spreadsheet as dd/MM/yyyy
  var day = new Date(event_values[7].toLocaleString()).getDate().valueOf();
  var month=new Date(event_values[7].toLocaleString()).getMonth().valueOf();
  var year=new Date(event_values[7].toLocaleString()).getFullYear().valueOf();
  // event_values[8] and [9] correspond to the start and end time of the event as HH:mm:ss also provided by the form to the spreadsheet
  var start_time=event_values[8].toString().split(":");
  // since the day was not provided for the start_time and end_time variables, in my case the script assumed as 1899. So I split the string first with ":" and then with "1899 ".
  var start_hour=start_time[0].split("1899 ")[1].valueOf();
  var start_min=start_time[1].valueOf();
  var end_time=event_values[9].toString().split(":")
  var end_hour=end_time[0].split("1899 ")[1].valueOf();
  var end_min=end_time[1].valueOf();

  var event_start_time = new Date(year,month,day,start_hour,start_min,"00");
  var event_end_time=new Date(year,month,day,end_hour,end_min,"00");

【讨论】:

    猜你喜欢
    • 2014-07-09
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2022-07-27
    • 1970-01-01
    相关资源
    最近更新 更多