使用 Google Sheets 电子表格上的 CalendarApp 在 Google Sheets 上写入日历事件 ID

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

我正在编写一个应用程序脚本代码来将电子表格中的事件写入我的 Google 日历,但我想将事件 ID 保存在范围右侧。我可以毫无问题地获取事件 id,但我不知道如何将其写入 H 列,因为我使用的是 forEach(entry) 循环。

这是我当前的js代码:

// Get calendar id from calendar settings
  calendar_id = [calendar id]
  // Create your Calendar App connection
  let myCalendar = CalendarApp.getCalendarById(calendar_id);
  // Get current sheet
  let sheet = SpreadsheetApp.getActiveSheet();
  // Get data range and values from current sheet
  let schedule = sheet.getDataRange().getValues()
  schedule.splice(0, 1)

  // Create events from each row in the sheet
  schedule.forEach(function(entry) {
    task_name = entry[0]
    task_start = new Date(entry[1])
    task_end = new Date(entry[2])
    guest_list = entry[6]
    event = myCalendar.createEvent(task_name, task_start, task_end, {guests: guest_list});
    event_id = event.getId();
    // How do I make it so that the event id will be written on what would be 'entry[7]'?
  });
}
如何将id添加到H列?

这是我的电子表格的格式:

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

使用累积数组构建整列值,并使用 setValues() 将其全部放入最后的工作表中:

// Get calendar id from calendar settings
  calendar_id = [calendar id]
  // Create your Calendar App connection
  let myCalendar = CalendarApp.getCalendarById(calendar_id);
  // Get current sheet
  let sheet = SpreadsheetApp.getActiveSheet();
  // Get data range and values from current sheet
  let schedule = sheet.getDataRange().getValues()
  schedule.splice(0, 1)

  let eid = [];

  // Create events from each row in the sheet
  schedule.forEach(function(entry) {
    task_name = entry[0]
    task_start = new Date(entry[1])
    task_end = new Date(entry[2])
    guest_list = entry[6]
    event = myCalendar.createEvent(task_name, task_start, task_end, {guests: guest_list});
    event_id = event.getId();
    // How do I make it so that the event id will be written on what would be 'entry[7]'?
    eid.push([event_id]);
  });
  sheet.getRange(2,8,eid.length,1).setValues(eid);
}

0
投票
function myFunk() {
  let myCalendar = CalendarApp.getCalendarById("calendar_id");
  let sh = SpreadsheetApp.getActiveSheet();
  let vs = sh.getDataRange().getValues()
  vs.splice(0, 1)
  let col8 = vs.map(function (r) {
    task_name = r[0]
    task_start = new Date(r[1])
    task_end = new Date(r[2])
    guest_list = r[6]
    event = myCalendar.createEvent(task_name, task_start, task_end, { guests: guest_list });
    event_id = event.getId();
    return [eveht_id];
  });
  sh.getRange(2, 8, col8.length, col8[0].length).setValues(col8)
}
© www.soinside.com 2019 - 2024. All rights reserved.