[英]Google Apps Script - Create Calendar Event from Spreadsheet
I have near 0 experience in programming.我在编程方面的经验接近 0。 I would like to import a range from Google Spreadsheets as an array, check each value in the array for True or False, and if True, create an All Day Calendar Event with the Title of the All Day Calendar Event being the value from the same row in a different array.我想从谷歌电子表格中导入一个范围作为数组,检查数组中的每个值的真或假,如果为真,创建一个全天日历事件,全天日历事件的标题是相同的值行在不同的数组中。
Example:例子:
Table桌子
Should create Calendar Events named "A" on 5/11 and 5/13, "B" on 5/11, and "C" on 5/11, 5/12, and 5/13应该在 5/11 和 5/13 上创建名为“A”的日历事件,在 5/11 上创建名为“B”的日历事件,在 5/11、5/12 和 5/13 上创建名为“C”的日历事件
So far, this has worked, but is not efficient.到目前为止,这已经奏效,但效率不高。 Can someone help me optimize this?有人可以帮我优化吗?
var Checked = spreadsheet.getRange("C5:I20").getValues();
var Person = spreadsheet.getRange("B5:B20").getValues();
var mon = new Date(Object(spreadsheet.getRange("C3").getValues()));
var tue = new Date(Object(spreadsheet.getRange("D3").getValues()));
var wed = new Date(Object(spreadsheet.getRange("E3").getValues()));
var thu = new Date(Object(spreadsheet.getRange("F3").getValues()));
var fri = new Date(Object(spreadsheet.getRange("G3").getValues()));
var sat = new Date(Object(spreadsheet.getRange("H3").getValues()));
var sun = new Date(Object(spreadsheet.getRange("I3").getValues()));
for (x=0; x<Checked.length; x++) {
for (x=0; x<Person.length; x++) {
var shift = Checked[x];
var Names = Person[x];
if(shift[0] = 1)
eventCal.createAllDayEvent(Object(Names), mon);
if(shift[1] = 1)
eventCal.createAllDayEvent(Object(Names), tue);
if(shift[2] = 1)
eventCal.createAllDayEvent(Object(Names), wed);
if(shift[3] = 1)
eventCal.createAllDayEvent(Object(Names), thu);
if(shift[4] = 1)
eventCal.createAllDayEvent(Object(Names), fri);
if(shift[5] = 1)
eventCal.createAllDayEvent(Object(Names), sat);
if(shift[6] = 1)
eventCal.createAllDayEvent(Object(Names), sun);
Utilities.sleep(1000)
Maybe this will help you to get started:也许这会帮助你开始:
This function will create all day events for all of the checked rows in the spreadsheet at the bottom.此 function 将为底部电子表格中所有选中的行创建全天事件。
function myfunction() {
const cal=CalendarApp.getCalendarById('id');
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');
const rg=sh.getRange('B5:E20');
var vA=rg.getValues();
vA.forEach(function(r,i){
if(r[0]) {
cal.createAllDayEvent(r[1], new Date(r[2]), new Date(r[3]))
}
});
}
Spreadsheet:电子表格:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.