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

ors*_*iro 4 google-sheets google-apps-script

假设我想清除G2:GXGX与最后一个非空单元格相对应的列范围中找到的所有值。

Pie*_*ard 7

看看Range.clear() 方法

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();

}
Run Code Online (Sandbox Code Playgroud)

或者如果你想保留 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();

}
Run Code Online (Sandbox Code Playgroud)

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

clearColumn(7, 2); 
Run Code Online (Sandbox Code Playgroud)

或者

clearColumnA1Notation("G2");
Run Code Online (Sandbox Code Playgroud)


sha*_*mal 5

sheet.getRange("G1:G").clear();

此语法将清除从 G1 到最后一个可用 G 列值的列。如果你想从第三行之间清除它,那么你可以使用

sheet.getRange("G3:G").clear();