javascript - For 循环中的 Deleterow() 不会删除 GSheets 上的正确行
问题描述
无法找出为什么这不删除正确的行:我已经尝试了所有可能的组合(在我的脑海中)。
function deleteItem() {
var sourceSheet = 'ArquivoItens';
var destinationSheet = 'EditarItem';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName(sourceSheet);
var LastRowSource = source.getLastRow();
var LastColumnSource = source.getLastColumn();
var values = source.getRange(2,1,LastRowSource,LastColumnSource).getValues();
var csh = ss.getSheetByName(destinationSheet);
var productCode = csh.getRange("W5").getValue();
var productVersao = csh.getRange("AC4").getValue();
for (var i = (values.length)-1; i > 0; i--) {
if (values[i][0] == productCode && values[i][2] == productVersao) {
Logger.log(values[i]);
source.deleteRow(i);
}
}
}
请让我知道缺陷在哪里。欣赏它!
解决方案
function deleteItem() {
var sourceSheet = 'ArquivoItens';
var destinationSheet = 'EditarItem';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName(sourceSheet);
var LastRowSource = source.getLastRow();
var LastColumnSource = source.getLastColumn();
//LastRowSource-startrow+1
var values = source.getRange(2,1,LastRowSource-1,LastColumnSource).getValues();
var csh = ss.getSheetByName(destinationSheet);
var productCode = csh.getRange("W5").getValue();
var productVersao = csh.getRange("AC4").getValue();
for (var i = values.length-1; i >= 0; i--) {
if (values[i][0] == productCode && values[i][2] == productVersao) {
Logger.log(values[i]);
source.deleteRow(i+2);//i+startrow which is two in this case
}
}
}
推荐阅读
- asp.net-core - ASP.NET Core (3.0) Rangeprocessing 视频流
- javascript - 图表 js 数据标签样式
- java - XPath 选择前面的输入
- php - 如何从调试器(流明,RESTClient)通过 JSON 存储多条记录
- .net - .NET Framework 中的“意外令牌”
- python - 我应该在文件开头还是其他地方声明导入?
- c++ - 使用 C++ 中的开源应用程序捕获 Qualcomm Quectel 调制解调器的诊断帧数据
- theorem-proving - 精益中的有根据的递归
- reactjs - 如何在不更改 React.Component 的所有用法的情况下扩展 React Component 道具类型
- python - 分析时python3.x中出现意外的MemoryError