通过 Google 表单提交创建 PM 日历活动

问题描述 投票:0回答:1

下午好!这是我第一次发帖,让我承认我是一名专业教育家,但在剧本写作方面是业余爱好者。我使用此站点的一些指导编写了一个脚本,该脚本在每次提交表单时创建一个日历事件。该表格适用于我们学校团队的“早期发布”,因此我在这里制作了一个虚拟表作为示例:

https://docs.google.com/spreadsheets/d/1b-8FWWppyNqfycMmc4E3TLT4Fl7W-TJqoG3Zy6XSCkQ/edit?usp=sharing

。然后,日历事件将转到与教师共享的活动日历。我遇到的问题是提交的时间总是默认为上午,但大多数是下午。我尝试自己修复它,但没有成功。任何帮助将不胜感激!

function createCalEvent(e) {
  Logger.log(e);
  // this will return something like this :
  /*
  {values=[11/9/2014 22:30:00, serge, test descr, 11/7/2014, Before school | 7:30], 
  namedValues={Your Full Name=[serge], Work to Make Up=[test descr], Date You Will Make Up Assignment=[11/7/2014], 
  Makeup Time=[Before school | 7:30], Timestamp=[11/9/2014 22:30:00]}, range=Range, source=Spreadsheet, authMode=FULL}
  */
  var cal = CalendarApp.getCalendarById("c_2d83fb444341942e6e44b791b97bd5db57611ab24aebfc16ed17a8b4a5ca0ad2@group.calendar.google.com");// replace with the right calendar ID, this one is for test (and is public)
  var name = e.namedValues["Coach/Advisor Name"][0];
  var team = e.namedValues["Team/Club/Organization"][0];
  var descr = e.namedValues["Name of Event"][0];
  var location = e.namedValues["Location to be released to"][0];
  var date = e.namedValues["Date of Event"][0].split('/');
  var time = e.namedValues["Time for Release"][0].split(':');
  var hrs = time[0];
  var mnts = time[1];
  Logger.log(name+' '+descr+' '+date[1]+' '+time[1]); // this will return  serge test descr 11,7,2014  7,30
  var startTime = new Date(date[2],date[0]-1,date[1]);
        var hours = hrs.toString();
        var minutes = mnts.toString();
  startTime.setHours(hours,minutes,0,0);
  endTime = new Date(startTime.getTime()+900000); //assuming event is 1 hour long
  Logger.log('start='+startTime+'  end='+endTime);
  cal.createEvent('Early Release for '+team, startTime, endTime, {'description':descr+' submitted by '+name+ '(please release to '+location+')'});
   cal.addEmailReminder(15);
   cal.addPopupReminder(15);
}

我尝试创建一个“if”语句,其中 if 小时 < 4, then +12 but it ended up moving the events to the wrong days and still not the right time. With the code as it is now, I get the right day, and hour, it's just the events are set to 2 or 3 am instead of pm.

google-apps-script google-sheets google-calendar-api google-forms
1个回答
0
投票

The issue I am having is the time's submitted always default to am but most are pm.

问题

脚本中的问题是在表单中输入的时间为 24 小时制,但在工作表/事件对象中记录为 12 小时制。

表格 片材
08:30 “上午 8:30:00”
15:30 “下午 3:30:00”

在工作表中,子午线标记位于时间的末尾(“AM”或“PM”)。这意味着无论发布时间是“AM”还是“PM”,所有小时值都在 1 到 12 之间。

此行:

var time = e.namedValues["Time for Release"][0].split(':');
捕获小时和分钟的分割,但不返回经络标记。

解决方案

  1. 捕捉经络标记:
    var timeMarker = e.namedValues["Release time"][0].slice(-2)
  2. 评估标记是否=“PM”
  • 如果是,加12
  • 如果没有,请使用原始时间

例如:

  • “8:30:00 AM”将返回小时数 = 8
  • “3:30:00 PM”返回时间 = 15

function releaseHours(e) {

  //Logger.log(JSON.stringify(e)) // DEBUG

  // get the Release time
  var time = e.namedValues["Release time"][0].split(':');
  // get the time marker (AM or PM)
  var timeMarker = e.namedValues["Release time"][0].slice(-2)
  Logger.log("DEBUG: RAW DATA: Time = "+time+", time marker = "+timeMarker)

  if (timeMarker === "PM"){
    var hrs = parseInt(time[0])+12
  }
  else{
    var hrs = parseInt(time[0])
  }
  Logger.log("DEBUG: Adjusted hours = "+hrs)
}

© www.soinside.com 2019 - 2024. All rights reserved.