Google日历到Google表格,脚本无法正确拾取时间

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

出于某种原因,我运行的脚本将事件时间缩短了一个小时。如果我的时间是下午4点,则将其调为下午3点。事件日期和时间与CST一样,但是显示不正确。是否需要更改此线以调出当地时间?

我不确定该怎么做。现在,使用CST拉取日期/时间,但是脚本行的输出带来了错误的时间。我可以添加一些其他内容吗?

var events = cal.getEvents(new Date("October 20, 2019 00:00:00 CST"), new Date("November 2, 2019 23:59:59 CST"), {search: ''});


var sheet = SpreadsheetApp.getActiveSheet();
// Uncomment this next line if you want to always clear the spreadsheet content before running - Note people could have added extra columns on the data though that would be lost
// sheet.clearContents();  

// Create a header record on the current spreadsheet in cells A1:N1 - Match the number of entries in the "header=" to the last parameter
// of the getRange entry below
sheet.clearContents();
var header = [["EVENT / SHOW", "VENUE / LOCATION", "DELIVERY NOTES", "DATE", "TIME"]]
var range = sheet.getRange(1,1,1,5);
range.setValues(header);


// Loop through all calendar events found and write them out starting on calulated ROW 2 (i+2)
for (var i=0;i<events.length;i++) {
var row=i+2;
var myformula_placeholder = '';
// Matching the "header=" entry above, this is the detailed row entry "details=", and must match the number of entries of the GetRange entry below
// NOTE: I've had problems with the getVisibility for some older events not having a value, so I've had do add in some NULL text to make sure it does not error
var details=[[events[i].getTitle(), events[i].getLocation(), events[i].getDescription(), events[i].getStartTime(), Utilities.formatDate(events[i].getStartTime(), Session.getScriptTimeZone(), "hh:mma")]];
var range=sheet.getRange(row,1,1,5);
range.setValues(details);

预期的结果是要使此时间准确显示在Google日历上。

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

仅供参考,这是我用来为您测试的代码

function runOne() {
  var ss=SpreadsheetApp.getActive();
  var calA=CalendarApp.getAllCalendars();
  var html='';
  var today=new Date();
  var s=new Date(today.getFullYear(),today.getMonth(),today.getDate());
  var e=new Date(today.getFullYear(),today.getMonth(),today.getDate()+15);
  var incl=getGlobal('includedCalendars').split('~~~');//includes a special subset of calendars because some of my calendars have a huge series of events to reminder to do regular things.
  calA.forEach(function(c){
    if(incl.indexOf(c.getName())>-1){
      html+=Utilities.formatString('<br />%s<br />',c.getName());
      var events=c.getEvents(s,e);  
      html+='<style>th,td{border:1px solid black;}</style><table>';
      html+=Utilities.formatString('<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th>',"EVENT/SHOW","VENUE/LOCATION","DELIVERY NOTES","DATE","TIME");
      events.forEach(function(ev){
        var sd=Utilities.formatDate(new Date(ev.getStartTime()),Session.getScriptTimeZone(),"E MM dd, yyyy");
        var st=Utilities.formatDate(new Date(ev.getStartTime()),Session.getScriptTimeZone(),"hh:mma");
        html+=Utilities.formatString('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',ev.getTitle(),ev.getLocation(),ev.getDescription(),sd,st);
      });
      html+='</table>';
    }
  });
  if(html) {
    var userInterface=HtmlService.createHtmlOutput(html).setWidth(1000);
    SpreadsheetApp.getUi().showModelessDialog(userInterface, "My Event Data");
  }else{
    SpreadsheetApp.getUi().alert('No Events Found');
  }
}

它将每个日历的所有事件数据加载到一个单独的小html表中,并在无模式对话框中显示所有事件数据。

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