我需要帮助让这个 "For" 循环和 "If" 语句工作

I need help getting this "For" loop and "If" statement to work

整个脚本依赖于 For 循环和 If 语句的两种组合。两者本质上是一样的。但出于某种原因,我可以让第一部分工作,但不能让第二部分工作。

我在下面附上了我的脚本和注释。谢谢。

/** @OnlyCurrentDoc */

function onEdit(e) {

//This part ensures the main script only runs if the cell F6 in the sheet "Daily Data" is edited.
  if (
    e.source.getSheetName() == "Daily Data" &&
    e.range.columnStart == 6 &&
    e.range.columnEnd == 6 &&
    e.range.rowStart >= 6 &&
    e.range.rowEnd <= 6 
  ) {

    //Secction 1: This section finds the lowest # from a list of numbers by finding the lowest empty cell in coloumn D using an If statement:
      var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
      var daily_data = spreadsheet.getSheetByName("Daily Data");
      var entirelistoftimes = daily_data.getRange(3, 4, 62).getValues();  
          for(var i=0; i<entirelistoftimes.length ; i++){  //This For loop will run through the entire D column in sheet "Daily Data".
            if (entirelistoftimes[i] == ""){        //This If statement will look for the first empty cell.
              //Copies the Total Number:
              var TotalNo = daily_data.getRange(i+2, 2).getValues();   //Gets the # from the cell next to the last filled cell in column D.
                spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Daily Data'), true);
              spreadsheet.getRange('F8').setValues(TotalNo);  //Displays the # in a cell F8 in sheet "Daily Data".
            //Stop once the first blank cell has been found:
            break;
          }
        }

    //THIS IS THE SECTION I CANNOT GET TO WORK:
    //Section 2: This section uses the # we got from the above section to find a time from a the corresponding row of sheet "Long Term Data":
      var LTD_data = spreadsheet.getSheetByName("Long Term Data");
      var LTD_data_entirelistoftimes = LTD_data.getRange(6, 2, 65).getValues();  
          for(var j=0; j<LTD_data_entirelistoftimes.length ; j++){  //This For loop will run through the Long Term Data sheet, through the list of numbers column.
            if (LTD_data_entirelistoftimes[j] == TotalNo){        //This if statement will look through column B from row 6 for the # we got from section 1 above.
              //Copies the time from column D in Lon:
              var YesterdayTime = LTD_data.getRange(j, 4).getValues();    //Gets the time from column D in row j in the "Long Term Data" Sheet.
                spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Daily Data'), true);
                spreadsheet.getRange('F9').setValues(YesterdayTime);  //Displays the time from the time underneath the # in sheet "Daily Data".
            //Stop once the above has been completed:
            break;
          }
        }
      }
}
;

如果你的脚本被修改了,下面的修改怎么样?

修改点:

  • 在您的脚本中,if (LTD_data_entirelistoftimes[j] == TotalNo){LTD_data_entirelistoftimes[j]TotalNo 分别是一维数组和二维数组。因为 getValues() returns 二维数组。我认为你的问题的原因是直接比较这些值。

修改后的脚本:

请按如下方式修改您的脚本。

模式 1:

==用于比较运算符时,可以修改如下。

从:
if (LTD_data_entirelistoftimes[j] == TotalNo){
到:
if (LTD_data_entirelistoftimes[j] == TotalNo[0][0]){

if (LTD_data_entirelistoftimes[j][0] == TotalNo[0][0]){

模式二:

当比较运算符使用===时,可以修改如下

从:
if (LTD_data_entirelistoftimes[j] == TotalNo){
到:
if (LTD_data_entirelistoftimes[j][0] === TotalNo[0][0]){

注:

  • if (entirelistoftimes[i] == ""){处,==被用作比较运算符。所以 if 语句有效。

参考文献:

如果此修改未能解决您的问题,我深表歉意。届时,您能否提供一个示例电子表格?至此,我想修改一下。