替换Google文档模板中的文本时,时间戳会发生变化

时间:2014-05-28 13:08:46

标签: google-apps-script google-calendar-api google-docs

我有一张Google表格供客户输入Catering的活动详情。然后,App Script会创建Google日历活动,打开Goog​​le文档模板以替换相应的文字,将文档转换为pdf并通过电子邮件向我发送一份副本,然后再将其从我的云端硬盘中删除。

在此处的代码中,日历事件使用“ var timeString ”。 重命名文档中的文字正在使用“ var timeFormat ”,以便不显示秒和GSM。他们都从电子表格中的“ time = newRequest [0] [14] ”单元格中提取数据。但由于某些原因我无法解释何时输入10:15,10:45,11:15,11:45,12:15和12:45,脚本会将文档模板上的时间更改为** :10,**:40。如果时间是一个数字小时,则不会发生这种情况。我不明白为什么会这样,请帮忙吗?

//rename data into useable tags  
  timestamp = newRequest[0][0],
  company = newRequest[0][1],
  contact_name = newRequest[0][2],
  contact_email = newRequest[0][3],
  contact_phone = newRequest[0][4],
  onsite_name = newRequest[0][5],
  onsite_phone = newRequest[0][6],
  pickup_delivery = newRequest[0][7],
  day = newRequest[0][8],
  address = newRequest[0][9],
  location = newRequest[0][10],
  parking = newRequest[0][11],
  guests = newRequest[0][12],
  other = newRequest[0][13],
  time = newRequest[0][14];
  date = newRequest[0][15]

//create new calendar event  
  var dateString = date.toDateString();
  var dateFormat = dateString.substr(4,11);
  var timeString = time.getHours() + ":" + time.getMinutes();
  var timeFormat = time.toLocaleTimeString().substr(0,4) + time.toLocaleTimeString().substr(7,4);
  var endtimeString = (time.getHours() + 1) + ":" + time.getMinutes();
  var calendar = CalendarApp.getCalendarById('haleyhouse.org_1oicreouarl3je785ltupuoppg@group.calendar.google.com');
  var event = calendar.createEvent(contact_name + ", " + company + ", " + contact_phone, 
      new Date(dateString + " " + timeString), 
      new Date(dateString + " " + endtimeString), 
      {description: timestamp + " received form request - google", location: address});

//rename text in the document
  copyBody.replaceText('keyDay', day);
  copyBody.replaceText('keyDate', dateFormat)
  copyBody.replaceText('keyTime', timeFormat)
  copyBody.replaceText('keyCompany', company);
  copyBody.replaceText('keyContact_Name', contact_name);
  copyBody.replaceText('keyContact_Email', contact_email);
  copyBody.replaceText('keyContact_Phone',contact_phone);
  copyBody.replaceText('keyOnsite_Name', onsite_name);
  copyBody.replaceText('keyOnsite_Phone', onsite_phone);
  copyBody.replaceText('keyDelivery', pickup_delivery);  
  copyBody.replaceText('keyAddress', address);
  copyBody.replaceText('keyLocation', location);
  copyBody.replaceText('keyGuests', guests);

1 个答案:

答案 0 :(得分:0)

在应用程序脚本中,Utilities.formatDate()是您的朋友。

试试这个:

  var startDate = dateString+' '+time;
  var timeFormat = Utilities.formatDate(new Date(startDate), Session.getScriptTimeZone(), 'hh:mm');
  Logger.log(timeFormat)

确保脚本项目属性设置为正确的时区。