批量请求删除空行和列

Batch request removing empty rows and columns

我需要创建一个脚本,从 1 column/row 开始的指示 sheet 中删除所有空行和列(row/column 的任何单元格中没有值),在同时 - 使用批量更新。我找到了一个脚本 并尝试使其适合我的需要。

我已经像下面那样修改了它,但是我在函数参数方面做错了(可能还有其他事情)。


function clear(){

  const sheetName = "Parser"; // Please set the sheet name.
  
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName(sheetName);
  const sheetId = sh.getSheetValues;
var values = sh.getRange('B1:B').getValues();
  var requests = values.reduce((ar, value) => {
    var maxColumns = sh.getMaxColumns(); 
    var lastColumn = sh.getLastColumn();
    var maxRows = sh.getMaxRows(); 
    var lastRow = sh.getLastRow();
    if (lastRow == 0 && lastColumn == 0 && maxRows > 1 && maxColumns > 1) {
     ar.push({deleteDimension: {range: {sheetId: sheetId, dimension: "ROWS", startIndex: 1}}});
     ar.push({deleteDimension: {range: {sheetId: sheetId, dimension: "COLUMNS", startIndex: 1}}});
    }
Logger.log(ar);
   return ar;
  }, []);
    if (requests.length > 0) {
    Sheets.Spreadsheets.batchUpdate({requests: requests}, id);
  }
};

求助!

来自:

致:

尝试:

function clean() {

  const sheetName = "Parser"
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)

  const cleanRows = sheet.getDataRange()
                         .getValues()
                         .filter(row => !row.every(cell => cell === ``))

  const cleanCols = cleanRows[0].map((_, index) => cleanRows.flatMap(row => row[index]))
                                .filter(col => !col.every(cell => cell === ``))

  const values = cleanCols[0].map((_, index) => cleanCols.flatMap(row => row[index]))

  sheet.getDataRange().clearContent()
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values)

  if (sheet.getLastRow() !== sheet.getMaxRows()) sheet.deleteRows(sheet.getLastRow()+1, sheet.getMaxRows()-sheet.getLastRow())
  if (sheet.getLastColumn() !== sheet.getMaxColumns()) sheet.deleteColumns(sheet.getLastColumn()+1, sheet.getMaxColumns()-sheet.getLastColumn())
      
}

这将过滤掉所有空行,旋转数组,过滤掉所有空行 'columns',然后将数组旋转回来并更新 sheet。

function clear() {
    
  const sheetName = "Parser"
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
    
  sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn()).clearContent()

}

让我知道这是否适合您!

清理()结果: