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



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

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

或者,如果要保留 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");

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

此语法将清除从 G1 到最后一个可用 G 列值的列。 如果要从第 3 行(例如(之间清除它,则可以使用

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

最新更新