在不删除整行的情况下删除Google表格中的单元格 [英] Deleting Cells in Google Sheets without removing a whole row
问题描述
我有以下代码,如果"delete"在该行的第4个单元格中,则当前将删除整个行.是否可以只删除左侧的3个单元格以及我在其中写入删除"的单元格,而不是整个行(例如,我不想删除单元格B16中的其他内容?)>
我无法找到.deleteCell函数.还有另一种方法吗?
function deleteRows(){var sheet = SpreadsheetApp.getActiveSheet();var rows = sheet.getDataRange();var numRows = rows.getNumRows();var values = rows.getValues();var rowsDeleted = 0;对于(var i = 0; i< = numRows-1; i ++){var row = values [i];if(row [4] =='delete'){//这将搜索A列中的所有单元格(更改为row [1]以获得B列,依此类推),并在单元格为空或值为'delete'时删除行.sheet.deleteRow((parseInt(i)+1)-rowsDeleted);rowsDeleted ++;}}};
问题是:
如何将给定单元格下面的所有单元格向上移动一列(覆盖给定的单元格值)?
执行此操作的代码为:
函数deleteCell(sheet,cellx,celly){var wholeSheet = SpreadsheetApp.openById(sheet).getRange("A1:F").getValues();for(var i = celly; i< wholeSheet.length; i ++){wholeSheet [cellx] [i] = WholeSheet [cellx] [i + 1];}SpreadsheetApp.openById(sheet).setValues(wholeSheet);}
I have the code as below that currently deletes the entire row if 'delete' is in the 4th cell of that row. Is it posible to just delete the 3 cells to the left as well as the cell I have written 'delete' in, rather than the whole row (I have other content in Cell B16 for example I don't want to delete?
I haven't been able to find a .deleteCell function. Is there another way to do this?
function deleteRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[4] == 'delete') { // This searches all cells in columns A (change to row[1] for columns B and so on) and deletes row if cell is empty or has value 'delete'.
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
So the problem is:
How do I move all the cells beneath a given cell one row up (overwriting the given cell value)?
The code to do this would be:
function deleteCell(sheet, cellx, celly){
var wholeSheet = SpreadsheetApp.openById(sheet).getRange("A1:F").getValues();
for(var i = celly; i<wholeSheet.length; i++){
wholeSheet[cellx][i] = wholeSheet[cellx][i+1];
}
SpreadsheetApp.openById(sheet).setValues(wholeSheet);
}
这篇关于在不删除整行的情况下删除Google表格中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!