仅删除某些值,而不是全部值

时间:2019-06-05 18:21:22

标签: google-apps-script google-sheets scripting

该脚本删除所选单元格中的所有值。如果该值是我不想删除的值,我想在例外中编写。

因为我身体不好,所以没有尝试过任何东西。

function Gtown() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('O38:P39').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('A38:B39').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('O26:P27').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('I26:J27').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('G26:H27').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('A26:B27').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('O16:P17').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('I16:J17').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('G16:H17').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('A16:B17').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('O6:P7').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('I6:J7').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('G6:H7').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('A6:B7').activate();
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
};

没有消息。我只想删除某些值。

2 个答案:

答案 0 :(得分:0)

首先,请使用循环!现在,您的脚本非常“繁重”:

function Gtown() {

  var ss   = SpreadsheetApp.getActive();
  var rl   = ss.getRangeList(['A1:A5','B1:B5']); //put any Range references here;
  var rs   = rl.getRanges();

  var opts = {contentsOnly: true, skipFilteredRows: true};

  rs.forEach(function(range){
    range.clear(opts);
  });

}

然后,在forEach“循环”内部,将clear()包裹在if...else语句中,而不是无条件清除(Range值可通过{{1访问}}或getValue()方法调用)

答案 1 :(得分:0)

尝试一下:

function clearSelectedLocations() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rA=['O38:P39','A38:B39','O26:P27','I26:J27','G26:H27','A26:B27','O16:P17','I16:J17','G16:H17','A16:B17','O6:P7','I6:J7','G6:H7','A6:B7'];
  var rgA=sh.getRangeList(rA).getRanges();
  for(var i=0;i<rgA.length;i++) {
    rgA[i].clear({contentsOnly: true, skipFilteredRows: true});
  }
}

尝试一下:

function clearSelectedLocations() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var vA=rg.getValues()
  for(var i=0;i<vA.length;i++) {
    if(sh.isRowHiddenByFilter(i+1)) {
      continue;
    }else{
      for(var j=0;j<vA[i].length;j++) {
        if(vA[i][j].toString()=="On Duty") {
          sh.getRange(i+1,j+1).clear({contentsOnly: true});
        }
      }
  }
}

如果要清除的值不止一个:

function clearSelectedLocations() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var clA=["On Duty"];
  for(var i=0;i<vA.length;i++) {
    if(sh.isRowHiddenByFilter(i+1)) {
      continue;
    }else{
      for(var j=0;j<vA[i].length;j++) {
        if(clA.indexOf(vA[i][j].toString())>-1) {
          sh.getRange(i+1,j+1).clear({contentsOnly: true});
        }
      }
  }
}