使用数组索引将值写入不同的工作表行

时间:2018-07-03 11:53:41

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

我目前正在尝试获取这段代码,以将事件从Google工作表发送到Google日历(贷给亚当·麦克法兰on this post)。

我的工作表目前大约有300行,并且正在增长,为了加快速度,我将范围设置为从第248行开始。但是这似乎使记录该事件为“完成”的部分无效。它将“在2个日历中”的值设置为第2、3、4和5行?!?

简单的解决方案是将范围重新设置为整个工作表,但我仍在学习。我想了解这里到底是什么无法正常工作,以及更多有关迭代如何工作的信息。

//mark as entered, enter ID
sheet.getRange(i+2, 32).setValue('In 2 calendar');

完整的代码如下:

function pushToCalendar() {
  //spreadsheet variables
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow(); 
  var range = sheet.getRange(248,1,lastRow,40);
  var values = range.getValues();   
  var updateRange = sheet.getRange('G1'); 

  var numValues = 0;
  for (var i = 0; i < values.length; i++) {     
    //check to see if name and type are filled out - date is left off because length is "undefined"
    if ((values[i][0].length > 0) && (values[i][2].length > 0)) {
      //check if it's been entered before          
      if (values[i][30] != 'In calendar') {    
        //Declare which calendar ID to use  (IGNORE THIS FOR NOW)                   
        var calendar = CalendarApp.getCalendarById('calendarID')

        // if (values [i][3] != 'Tropical 2450 Pontoon'){
        // var calendar = CalendarApp.getCalendarById('calendarID')

        //create event https://developers.google.com/apps-script/class_calendarapp#createEvent
        var newEventTitle = values[i][3]+'. '+values[i][2]+'. '+values[i][13]
                  +'. '+values[i][5]+'/'+values[i][6]+'/'+values[i][7]
                  +'. '+values[i][18]+' total, '+values[i][25]+' to pay. '+values[i][0];
        // var newEvent = calendar.createEvent('hello', Date[i][1], Date[i][5]);
        var newEvent = calendar.createEvent(newEventTitle, 
                                            //new Date(values[i][6]),
                                            new Date(values[i][32]),
                                            new Date(values[i][33]));
                                            //{guests:'tures.com.au', sendInvites: true});

        //mark as entered, enter ID
        sheet.getRange(i+2, 32).setValue('In 2 calendar');

      } //could edit here with an else statement
    }
    numValues++;
  }
}

0 个答案:

没有答案