我正在编写一个应用程序脚本代码来将电子表格中的事件写入我的 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]'?
});
}
使用累积数组构建整列值,并使用 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);
}
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)
}