簡體   English   中英

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

[英]How can I clear a column in Google Sheets using Google Apps Script?

假設我想清除列范圍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 列值的列。 如果你想從第三行之間清除它,那么你可以使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM