简体   繁体   English

如何使用应用程序脚本删除特定列中的空单元格?

[英]How to delete the empty cells in a specific column using apps script?

The goal is to delete empty cells in Column N alone, without disturbing the other columns and shift the rest of the cells upwards to obtain a compact column with just non empty cells.目标是单独删除第 N 列中的空单元格,而不会干扰其他列,并将其余单元格向上移动以获得只有非空单元格的紧凑列。 There can and will be empty cells after the result of course.当然,在结果之后可以并且将会有空单元格。 Please suggest a method请推荐一个方法

function Defaulters() {
    var spreadsheet = SpreadsheetApp.getActive();
    var as = spreadsheet.getActiveSheet();
 

    //to get the last row of the Column
      
    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);
  
    //to delete the empty cells and give a compact output of just names and emails in the Column

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

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

     else

     Logger.log(false);**// what to put here to retain this cell?**
  }
}

I'd try something like this:我会尝试这样的事情:

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
}

Or even like this:或者甚至像这样:

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

If you need to keep all the table intact and remove empty cells only from column 'N' it can be done this way:如果您需要保持所有表格完好无损并仅从“N”列中删除空单元格,则可以通过以下方式完成:

function clean_column_N() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const range = sheet.getRange('N3:N'+sheet.getLastRow())       // get a range start from row 3
  const data = range.getValues().filter(String);                // get a data and remove empty elements
  range.clearContent().offset(0,0,data.length).setValues(data); // put the data back on the sheeet
}

All data in column 'N' will be moved upward. “N”列中的所有数据都将向上移动。

Update更新

Modified last variant to clean any column:修改最后一个变体以清理任何列:

function main() {
  clean_column('N');
  clean_column('O');
}

function clean_column(col) {
  const sheet = SpreadsheetApp.getActiveSheet();
  const range = sheet.getRange(col + '3:' + col + sheet.getLastRow());
  const data = range.getValues().filter(String);
  range.clearContent().offset(0,0,data.length).setValues(data);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将公式拖入空单元格(仅限一列)? Google Apps脚本 - How to drag down a formula into empty cells (one column only)? Google Apps Script 如何使用 getLastRow() & shift 删除谷歌应用程序脚本中列范围内的单元格 - How to delete cells in a range of a column in google app script using getLastRow() & shift 如何使用 Google 应用程序脚本复制和粘贴“Over the Cells”图像? - How to copy and paste "Over the Cells" images using Google apps script? 如何使用 Google Apps 脚本对 10 个单元格进行分组 - How can I group 10 cells using Google Apps Script 如何使用单元格的应用程序脚本将 Google 表格数据验证添加到列中,以仅允许输入颜色和十六进制颜色代码 - How do I add Google sheets data vaildation to a column using apps script for cells to only allow colors and hex color codes to be inputted 应用程序脚本 | 如何根据列中的值归档特定行 - Apps Script | How to Archive Specific Rows Based On Values In Column 如何避免使用 Google Apps 脚本在循环中出现空 object 错误? - How to avoid empty object error in a loop using Google Apps Script? 如何使用 Google Apps 脚本根据名称标准删除工作表? - How to delete sheets based on a name criteria using Google Apps Script? 如何使用应用程序脚本来计算单元格数量并根据它更改其他列中的上下文? - How to use apps script to count cells number and change the context in other column based on it? 如果仅使用电子表格脚本的特定列中包含字符串,如何删除行 - How to delete rows if contains a string only in a specific column with spreadsheet script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM