如何使用Google Apps脚本清除Google表格中的列?

时间:2017-08-08 08:18:50

标签: google-apps-script google-sheets

假设我要清除列范围G2:GX中找到的所有值,其中GX对应于最后一个非空的单元格。

2 个答案:

答案 0 :(得分:4)

看看Range.clear() method

function clearColumn(colNumber, startRow){
  //colNumber is the numeric value of the colum
  //startRow is the number of the starting row

  var sheet = SpreadsheetApp.getActiveSheet();
  var numRows = sheet.getLastRow() - startRow + 1; // The number of row to clear
  var range = sheet.getRange(startRow, colNumber, numRows);
  range.clear();

}

或者如果你想保留A1表示法:

function clearColumnA1Notation(a1Notation){
  //a1Notation is the A1 notation of the  first element of the column

  var sheet = SpreadsheetApp.getActiveSheet();
  var firstCell = sheet.getRange(a1Notation);
  var numRows = sheet.getLastRow() - firstCell.getRow() + 1;
  var range = sheet.getRange(firstCell.getRow(), firstCell.getColumn(), numRows);
  range.clear();

}

对于您的示例,您可以使用:

clearColumn(7, 2); 

clearColumnA1Notation("G2");

答案 1 :(得分:1)

sheet.getRange(“ G1:G”)。clear();

此语法将从G1清除列到最后一个可用的G列值。您想从第三行之间清除ID,然后可以使用

sheet.getRange(“ G3:G”)。clear();