跳过循环中具有直通

时间:2018-01-22 07:01:32

标签: google-apps-script google-sheets

背景:我使用电子表格在日历中创建活动,以便为我的影院制作时间表。因为我们的节目不断更新和转变,我选择使用我在这里发现的一个例子,每次根据eventId删除和重建事件,它的效果很好。我实施的另一个元素是每晚它都会进行检查,并为过去的任何事件添加直线和斜体。我很乐意调整创建事件的功能,以跳过任何斜体或直线操作,以便过去的事件保持不变。

TlDr: 如何跳过具有" line-through"的行在我的for循环中?

这就是我现在所拥有的:

   /**
 * Export events from spreadsheet to calendar
 */
function exportPracticeEvents() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 1;  // Number of rows of header info (to skip)
  var range = sheet.getDataRange();
  var lc = sheet.getLastColumn()
  var data = range.getValues();
  var strike = range.getFontLine();
  Logger.log(strike);
  var calId = "24thstreet.org_eri1dhhin8rnelpg2kuoacklnk@group.calendar.google.com"; //practice
  var cal = CalendarApp.getCalendarById(calId);
  for (i=0; i<data.length; i++) {
    if (i < headerRows) continue; // Skip header row(s) and rows with line-through
    if (range.getFontLine() === "line-through") { continue; };
    var row = data[i];
    var date = new Date(row[0]);  // First column
    var type = row [1];
    var title = row[13];           // Fourteenth column
    var tstart = new Date(row[3]);
    tstart.setDate(date.getDate());
    tstart.setMonth(date.getMonth());
    tstart.setYear(date.getYear());
    var tstop = new Date(row[4]);
    tstop.setDate(date.getDate());
    tstop.setMonth(date.getMonth());
    tstop.setYear(date.getYear());
    var loc = "1117 w 24th Street, Los Angeles, CA 90007";
    var desc = row[14];
    var id = row[15];              // Sixteenth column == eventId actual
    // Check if event already exists, delete it if it does
    try {
      var event = cal.getEventById(id)
      event.deleteEvent();
      row[15] = '';  // Remove event ID    
      }
    catch (e) {
        // do nothing - we just want to avoid the exception when event doesn't exist
      }
    //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
    //var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}).getId();
    //row[15] = newEvent;  // Update the data array with event ID
    //debugger;
    if (type == "NO ESR") {
      cal.createAllDayEvent(title, new Date("March 3, 2010"));
      var newEvent = cal.createAllDayEvent(title, date).getId();
      row[15] = newEvent;  // Update the data array with event ID

    }

    else {
      cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
      var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}).getId();
      row[15] = newEvent;  // Update the data array with event ID

    }
     debugger;

    }
// Record all event IDs to spreadsheet
var id_data = data.map(function (row) {
  return [row[15]];      // keep only that column
});
sheet.getRange(1, 16, id_data.length, 1).setValues(id_data);  // write it in the sheet
}

我的&#34; if(range.getFontLine()===&#34; line-through&#34;){continue; }; &#34;似乎没有做任何事情。我如何使它工作?

1 个答案:

答案 0 :(得分:0)

如何修改如下?添加了一行,修改了一行。检索字体行数据的范围与data相同,并反映到for循环。

我从您的脚本中了解到,row[15]是列&#34; P&#34;是应该搜索&#34; line-through&#34;的值。我的理解是否正确?

修改后的脚本:

var fontLines = range.getFontLines(); // <--- Added
for (i=0; i<data.length; i++) {
  if (i < headerRows) continue; // Skip header row(s) and rows with line-through
  if (fontLines[i][15] == "line-through") { continue; }; // <--- Modified

注意:

  • 我使用您脚本的一部分显示了此修改部分。如果你很难理解这一点,请告诉我。

参考:

如果我误解了你的问题,我很抱歉。