简体   繁体   English

如何使用 getLastRow() & shift 删除谷歌应用程序脚本中列范围内的单元格

[英]How to delete cells in a range of a column in google app script using getLastRow() & shift

I've been looking through stackoverflow for some help on how to script in google sheets a function that would allow me to remove empty cells in a range and shift them over (to the left) when completed.我一直在通过 stackoverflow 寻找一些关于如何在谷歌表格中编写脚本的帮助 function 这将允许我删除一个范围内的空单元格并在完成后将它们移到(向左)。 I've only seen questions regarding removing empty rows or columns instead of cells in a range.我只看到有关删除空行或空列而不是范围内的单元格的问题。

This is what the data looks like I'm trying to work with:这就是我正在尝试处理的数据:

example google sheet谷歌表格示例

I need to delete cells A1-A15 and shift over the other columns .我需要删除单元格 A1-A15 并移过其他列

What's currently working is the following code, however it will delete/shift over the column even if there is data in the column (A) that I'm having the function review (ex. data in cell A6).目前正在运行的是以下代码,但是它会删除/移动该列,即使列 (A) 中有我正在进行 function 审核的数据(例如单元格 A6 中的数据)。

I want to ensure that this function doesn't go off if there is data in column A before the last and 2nd to last data entry as per the example.如果按照示例,在最后一个和倒数第二个数据条目之前的 A 列中有数据,我想确保这个 function 不会关闭 go。 : :

 function blankRemoval(){ var sheet = SpreadsheetApp.getActive().getSheetByName('sheet1');//need to call sheet by name as I have multiple tabs var lastRow = sheet.getLastRow() var cellsBlank = sheet.getRange(1,1).isBlank(); if(cellsBlank) { sheet.getRange(1,1,lastRow-3,1).deleteCells(SpreadsheetApp.Dimension.COLUMNS); // shifts cells left }//end of if }//end of function

So with this in mind, I thought I could make the variable cellsBlank to use.getRange(1,1,lastRow-3,1) as well so that it only looks through the range to the last row, and stops 3 from the last row.因此,考虑到这一点,我想我也可以将变量 cellsBlank 设置为 use.getRange(1,1,lastRow-3,1) 以便它只查看范围到最后一行,并从最后一行停止 3排。 However when I use this code, it doesn't delete the cells if there is data in them or not (the function runs without errors but nothing occurs).但是,当我使用此代码时,无论是否有数据,它都不会删除单元格(function 运行时没有错误,但没有任何反应)。

I've tried a few different items to use on this but I'm running into a wall as I'm not a proficient programmer.我已经尝试了几个不同的项目来使用它,但我遇到了困难,因为我不是一个熟练的程序员。 My guess is that it needs to iterate, or I'm doing something wrong with the range.我的猜测是它需要迭代,或者我在范围内做错了什么。

Also to note, there will always be 2 data entries in column A like my example, with data being one cell apart.还要注意,像我的示例一样,A 列中始终有 2 个数据条目,数据之间相隔一个单元格。 This is a entry of sorts to help with historical review.这是一个有助于历史回顾的条目。 I want to retain these two cells where they are, but shift the empty data above我想将这两个单元格保留在原处,但将上面的空数据移动

Probably it should be this:大概应该是这样的:

function blankRemoval(){
  var sheet = SpreadsheetApp.getActive().getSheetByName('sheet1');
  var col_A = sheet.getRange(1,1,sheet.getLastRow()-3,1);

  if (col_A.getValues().flat().filter(String).length == 0) { // <-- if the col A is empty
    col_A.deleteCells(SpreadsheetApp.Dimension.COLUMNS);
  }
}

.getValues() get a 2d array with values .getValues()获取带有值的二维数组

.flat() convert the 2d array into an 1d array .flat()将二维数组转换为一维数组

.filter(String) remove from the array all empty cells .filter(String)从数组中移除所有空单元格

.length get a length of the array .length得到数组的长度

This way, if the range contains empty cells only the array will have no elements (array.length = 0)这样,如果范围包含空单元格,则数组将没有元素 (array.length = 0)

Before:前:

在此处输入图像描述

After:后:

在此处输入图像描述

Update更新

It turned out that some cells in column 'A' could contain spaces.事实证明,“A”列中的某些单元格可能包含空格。 Here is modified function that removes spaces from the range:这里修改了 function,从范围中删除了空格:

function blankRemoval(){
  var sheet = SpreadsheetApp.getActive().getSheetByName('sheet1');
  var col_A = sheet.getRange(1,1,sheet.getLastRow()-3,1);

  var contents = col_A.getValues().flat().join('').replace(/\s+/g,'');

  if (contents == '') col_A.deleteCells(SpreadsheetApp.Dimension.COLUMNS);
}

This seems to be the best function to use for the question I posed thanks to @Yuri.这似乎是最好的 function 用于解决我向@Yuri 提出的问题。 The clearContent helps remove any entries that would prevent the deleteCells action to go clearContent 有助于删除任何会阻止对 go 执行 deleteCells 操作的条目

 function blankRemoval(){ var sheet = SpreadsheetApp.getActive().getSheetByName('sheet1'); var col_A = sheet.getRange(1,1,sheet.getLastRow()-3,1); if (col_A.getValues().flat().filter(String).length == 0) { col_A.clearContent();} //<---checks if cell range is empty, and if so performs a clear content (this seemed to fix issues with any ghost entries) if (col_A.getValues().flat().filter(String).length == 0) { // <-- if the col A is empty col_A.deleteCells(SpreadsheetApp.Dimension.COLUMNS); } }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM