脚本,删除问?并删除

时间:2017-07-20 18:00:26

标签: google-apps-script

不熟悉Google脚本,只是想知道如何将此部分更改为只删除而不是询问我是否要删除,基本上只是说是。



// Remove any calendar events not found in the spreadsheet
  var numToRemove = calEventIds.reduce(function(prevVal, curVal) {
    if (curVal !== null) {
      prevVal++;
    }
    return prevVal;
  }, 0);
  if (numToRemove > 0) {
    var ui = SpreadsheetApp.getUi();
    var response = ui.Button.YES;
    if (numToRemove > numUpdated) {
      response = ui.alert('Delete ' + numToRemove + ' calendar event(s) not found in spreadsheet?',
          ui.ButtonSet.YES_NO);
    }
    if (response == ui.Button.YES) {
      calEventIds.forEach(function(id, idx) {
        if (id != null) {
          calEvents[idx].deleteEvent();
          Utilities.sleep(20);
        }
      });
    }




2 个答案:

答案 0 :(得分:0)

如果有效,请告诉我,我只是将您的代码移到if语句中。

// Remove any calendar events not found in the spreadsheet
var numToRemove = calEventIds.reduce(function(prevVal, curVal) {
  if (curVal !== null) {
    prevVal++;
  }
  return prevVal;
}, 0);

if (numToRemove > 0) {
  var ui = SpreadsheetApp.getUi();
  var response = ui.Button.YES;
  if (numToRemove > numUpdated) {
    calEventIds.forEach(function(id, idx) {
      if (id != null) {
        calEvents[idx].deleteEvent();
        Utilities.sleep(20);
      }
    });
  }
}

答案 1 :(得分:0)

在删除项目之前,将删除方法放在if (numToRemove > 0) {块内,前提是这样,所以它看起来像:

if (numToRemove > 0) {  
  calEventIds.forEach(function(id, idx) {
    if (id != null) {
      calEvents[idx].deleteEvent();
      Utilities.sleep(20);
    }
  });
}