如何使用apps脚本删除特定列中的空单元格?如果更好,请建议其他方法

kuarbcqp  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(298)
function Defaulters() {
var spreadsheet = SpreadsheetApp.getActive();
var as = spreadsheet.getActiveSheet();

//获取列的最后一行

var lastRow = 100;
var range = as.getRange("N" + lastRow);

if (range.getValue() !== "") 

{Logger.log(lastRow);}

{lastRow = range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();}

Logger.log(lastRow);

//删除空单元格并在列中提供姓名和电子邮件的压缩输出

for (var l=lastRow; l>=3; l--)
{if(as.getRange(l,14).getValue() == "")

 Logger.log(true);**//what to put here to delete this row?**

 else

 Logger.log(false);**// what to put here to retain value?**

} }

ggazkfy8

ggazkfy81#

我想试试这样的东西:

function myFunction() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const data = sheet.getDataRange().getValues(); // get all data
  const data_new = data.filter(row => row[13] != ''); // filter the data by column 'N'
  sheet.clearContents(); // clean the sheet
  sheet.getRange(1,1,data_new.length,data_new[0].length)
    .setValues(data_new); // put the new data back on the sheet
}

甚至像这样:

function myFunction() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const data = sheet.getDataRange().getValues().filter(row => row[13] != '');
  sheet.clearContents().getRange(1,1,data.length,data[0].length).setValues(data);
}

相关问题