gpt4 book ai didi

javascript - 为什么 .indexof() 返回 -1;当该项目存在于给定数组中时? |谷歌脚本

转载 作者:行者123 更新时间:2023-11-28 03:25:12 25 4
gpt4 key购买 nike

-SheetData 变量仅获取当前工作表上给定范围的前 2 列。

-WeeksRow - 在单独的工作表“总计”上;我有一年中每周的开始日期,例如:“1 月 1 日”、“1 月 8 日”、“1 月 15 日”等。此数组 (WeeksRow) 获取这一行。

function ImportData(){
var SheetData = [{}];
var WeeksRow = [{}];

SheetData = sheet.getRange(4, 1, sheet.getDataRange().getLastRow()-4, 1).getValues();


var CurrentSheetName = Utilities.formatDate(sheet.getRange("B2").getValue(),spreadsheet.getSpreadsheetTimeZone(),"dd MMMM");

var TotalsSheet = spreadsheet.getSheetByName("Totals");

WeeksRow = TotalsSheet.getRange(1, 1, 1, TotalsSheet.getLastColumn()).getDisplayValues();

WeeksRow = String(WeeksRow).split(",").map(function(s) {return s.trim();});

var ColumnToSelect = WeeksRow.indexOf(CurrentSheetName);
Logger.log(CurrentSheetName)
Logger.log(WeeksRow)
Logger.log(ColumnToSelect)
<小时/>

执行后日志输出为:

[19-11-04 20:26:31:457 EST] 22 January

[19-11-04 20:26:31:458 EST] [Week Starting:, 01 January, , , , , 08 January, , , , , 15 January, , , , , 22 January, , , , , 29 January, , , , , 05 February, , , , , 12 February, , , , , 19 February, , , , , 26 February, , , , , 05 March, , , , , 12 March, , , , , 19 March, , , , , 26 March, , , , , 02 April, , , , , 09 April, , , , , 16 April, , , , , 23 April, , , , , 30 April, , , , , 07 May, , , , , 14 May, , , , , 21 May, , , , , 28 May, , , , , 04 June, , , , , 11 June, , , , , 18 June, , , , , 25 June, , , , , 02 July, , , , , 09 July, , , , , 16 July, , , , , 23 July, , , , , 30 July, , , , , 06 August, , , , , 13 August, , , , , 20 August, , , , , 27 August, , , , , 03 September, , , , , 10 September, , , , , 17 September, , , , , 24 September, , , , , 01 October, , , , , 08 October, , , , , 15 October, , , , , 22 October, , , , , 29 October, , , , , 05 November, , , , , 12 November, , , , , 19 November, , , , , 26 November, , , , , 03 December, , , , , 10 December, , , , , 17 December, , , , , 24 December, , , , ]

[19-11-04 20:26:31:458 EST] -1.0

<小时/>

如您所见,我正在寻找的商品是 1 月 1 日;它显然存在于数组的第二个元素中;我正在尝试使用 .indexof()

那么为什么.indexof()命令返回-1.0

我还尝试针对数组中的每个元素搜索“01 January”,但找不到它。使用以下代码:

var i=0;
while(i <= WeeksRow.length){
if (String( WeeksRow[i]) == "01 January"){
Browser.msgBox("found at "+i);
break;
}else{
i++;
}
if (i == WeeksRow.length){
Browser.msgBox("not found")
}
}

最佳答案

WeeksRow 的每个元素的开头似乎都有空格,您需要在搜索之前将其去掉。

var WeeksRow = String(WeeksRow).split(",").map(function(s) {return s.trim();});

关于javascript - 为什么 .indexof() 返回 -1;当该项目存在于给定数组中时? |谷歌脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58701718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com