-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

我还尝试针对arry中的每个元素搜索“ 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();});

09-10 05:11