我试图弄清楚为什么我的函数在遍历循环时没有增加到列值范围内的下一个值。我将分享以供参考(下)。该函数旨在传递工作表和搜索键以在登台和生产工作表的两个列中查找值,并返回与两个工作表中的搜索键匹配的rowNum。然后删除相应的行。请查看日志和执行记录(以下代码)
function findInColumn(sheet, data)
{
if(sheet.getName() == "Data" || sheet.getName() == "Info")
{
var column = sheet.getRange("D" + ":" + "D"); // like A:A
var values = column.getValues();
var row = 0;
while ( values[row] && values[row][0] != data ) {
row++;
}
if (values[row][0] == data)
return row+1;
else
return -1;
}
else if(sheet.getName() == "AdData" || sheet.getName() == "AdInfo")
{
var columnID = sheet.getRange("C" + ":" + "C"),
columnEmail = sheet.getRange("I"+ ":" + "I"); // like A:A
var valuesID = [columnID.getValues()],
valuesEmail = [columnEmail.getValues()];
for(var row = 0;row<valuesID.length;row++)
{
if(valuesID[row][0] == data)
{
return Logger.log(row+1);
}
else if(valuesEmail[row][0] == data)
{
return Logger.log(row+1);
}
else
{
Logger.log("ID " + valuesID[row][0] + "\nEmail " + valuesEmail[row][0] + "\n");
continue;
}
}
Logger.log("No matching ID AND Email were found");
}
else
{
return Logger.log("No sheet was found with that name.");
}
}
执行成绩单:
[18-11-07 11:17:00:183 PST] SpreadsheetApp.getActiveRange()[0 秒]
[18-11-07 11:17:00:183 PST] Range.getRow()[0秒]
[18-11-07 11:17:00:183 PST] Range.getLastRow()[0秒]
[18-11-07 11:17:00:183 PST] Range.getColumn()[0秒]
[18-11-07 11:17:00:183 PST] Range.getLastColumn()[0秒]
[18-11-07 11:17:00:183 PST] SpreadsheetApp.getActiveSpreadsheet()[0 秒]
[18-11-07 11:17:00:190 PST]开始执行
[18-11-07 11:17:00:194 PST] Logger.log([成功!命名值网站 管理员选项:删除网站管理员,[]])[0秒]
[18-11-07 11:17:00:195 PST] SpreadsheetApp.getActiveSpreadsheet()[0 秒]
[18-11-07 11:17:00:304 PST] Spreadsheet.getSheetByName([Admin Data]) [0.109秒]
[18-11-07 11:17:00:305 PST] SpreadsheetApp.getActiveSpreadsheet()[0 秒]
[18-11-07 11:17:00:305 PST] Spreadsheet.getSheetByName([管理员信息]) [0秒]
[18-11-07 11:17:00:306 PST] Logger.log([123 test@test.com,[]])[0 秒]
[18-11-07 11:17:00:306 PST] Sheet.getName()[0秒]
[18-11-07 11:17:00:306 PST] Sheet.getName()[0秒]
[18-11-07 11:17:00:306 PST] Sheet.getName()[0秒]
[18-11-07 11:17:00:306 PST] Sheet.getRange([C:C])[0秒]
[18-11-07 11:17:00:307 PST] Sheet.getRange([I:I])[0秒]
[18-11-07 11:17:00:562 PST] Range.getValues()[0.255秒]
[18-11-07 11:17:00:740 PST] Range.getValues()[0.176秒]
[18-11-07 11:17:00:741 PST] Logger.log([ID网站ID电子邮件Admin电子邮件, []])[0秒]
[18-11-07 11:17:00:741 PST] Logger.log([没有匹配的SiteID和管理员 找到了电子邮件,[]])[0秒]
[18-11-07 11:17:00:742 PST] Sheet.getName()[0秒]
[18-11-07 11:17:00:742 PST] Sheet.getName()[0秒]
[18-11-07 11:17:00:742 PST] Sheet.getName()[0秒]
[18-11-07 11:17:00:742 PST] Sheet.getRange([C:C])[0秒]
[18-11-07 11:17:00:743 PST] Sheet.getRange([I:I])[0秒]
[18-11-07 11:17:01:001 PST] Range.getValues()[0.257秒]
[18-11-07 11:17:01:002 PST] Range.getValues()[0秒]
[18-11-07 11:17:01:004 PST] Logger.log([ID网站ID电子邮件Admin电子邮件, []])[0秒]
[18-11-07 11:17:01:004 PST] Logger.log([没有匹配的SiteID和管理员 找到了电子邮件,[]])[0秒]
[18-11-07 11:17:01:004 PST] Sheet.getName()[0秒]
[18-11-07 11:17:01:004 PST] Sheet.getName()[0秒]
[18-11-07 11:17:01:004 PST] Sheet.getName()[0秒]
[18-11-07 11:17:01:005 PST] Sheet.getName()[0秒]
[18-11-07 11:17:01:005 PST] Sheet.getRange([C:C])[0秒]
[18-11-07 11:17:01:006 PST] Sheet.getRange([I:I])[0秒]
[18-11-07 11:17:01:255 PST] Range.getValues()[0.248秒]
[18-11-07 11:17:01:556 PST] Range.getValues()[0.3秒]
[18-11-07 11:17:01:557 PST] Logger.log([ID网站ID电子邮件Admin电子邮件, []])[0秒]
[18-11-07 11:17:01:558 PST] Logger.log([没有匹配的SiteID和管理员 找到了电子邮件,[]])[0秒]
[18-11-07 11:17:01:558 PST] Sheet.getName()[0秒]
[18-11-07 11:17:01:558 PST] Sheet.getName()[0秒]
[18-11-07 11:17:01:558 PST] Sheet.getName()[0秒]
[18-11-07 11:17:01:558 PST] Sheet.getName()[0秒]
[18-11-07 11:17:01:558 PST] Sheet.getRange([C:C])[0秒]
[18-11-07 11:17:01:559 PST] Sheet.getRange([I:I])[0秒]
[18-11-07 11:17:01:817 PST] Range.getValues()[0.257秒]
[18-11-07 11:17:02:035 PST] Range.getValues()[0.217秒]
[18-11-07 11:17:02:036 PST] Logger.log([ID网站ID电子邮件Admin电子邮件, []])[0秒]
[18-11-07 11:17:02:037 PST] Logger.log([没有匹配的SiteID和管理员 找到了电子邮件,[]])[0秒]
[18-11-07 11:17:02:037 PST] Sheet.getName()[0秒]
[18-11-07 11:17:02:037 PST] Sheet.getName()[0秒]
[18-11-07 11:17:02:037 PST] Sheet.getName()[0秒]
[18-11-07 11:17:02:038 PST] Sheet.getRange([C:C])[0秒]
[18-11-07 11:17:02:038 PST] Sheet.getRange([I:I])[0秒]
[18-11-07 11:17:02:316 PST] Range.getValues()[0.278秒]
[18-11-07 11:17:02:318 PST] Range.getValues()[0秒]
[18-11-07 11:17:02:320 PST] Logger.log([ID网站ID电子邮件Admin电子邮件, []])[0秒]
[18-11-07 11:17:02:320 PST] Logger.log([没有匹配的SiteID和管理员 找到了电子邮件,[]])[0秒]
[18-11-07 11:17:02:324 PST]执行失败:找不到方法 deleteRow((class))。 (第113行,文件为“宏”)[总计2.127秒 运行时]
答案 0 :(得分:0)
替换
var valuesID = [columnID.getValues()],
valuesEmail = [columnEmail.getValues()];
作者
var valuesID = columnID.getValues(),
valuesEmail = columnEmail.getValues();
在这里,您尝试递增3D数组,就像2D数组一样。 getValues
已返回2D数组。
参考