为什么 google sheet 脚本会更改我不想更改的行?

Why google sheet script changes rows I don't want it to change?

我想更改数组中行的值,当它与上一行具有相同的居民和相同的药物时。

最初它会查看它是否是重复任务 - 如果是,则设置默认值 "More":

//check if something from one time per day
    if (startTime[resMedVals[resMedRow][3]]){
      medPlan[lastRow].push(startTime[resMedVals[resMedRow][3]]+":00");                
    }else{
      medPlan[lastRow].push("More")
    }//end if time

然后它返回并处理 "More" 值。如果是第一次重复任务,则设置初始开始时间值。如果重复,则根据需要复制尽可能多的行。

//if more times per day medicine, then replicate tasks
    if(medPlan[lastRow][9]== "More"){
      var timesToReplicate = medPlan[lastRow][5].charAt(0)

      for(var i=0; i < timesToReplicate; i++){
        if (i==0){
          //first time
          medPlan[lastRow][9] = workDay["start"]
          medPlan[lastRow][9] += ":00"

        }else{

          //add entire last entire row
          medPlan.push(medPlan[medPlan.length-1]);

        }//end if else first time

直到这部分一切正常。

然后我想为重复的(而不是第一个)值设置不同的值。

    //adjust More values to appropriate starting times
  for (var med=1; med < medPlan.length; med++){
    if(medPlan[med][2] == medPlan[med-1][2] && medPlan[med][1] == medPlan[med-1][1]){
      Logger.log(med)
      var interval =  Math.floor(workingHours / medPlan[med][5].charAt(0));

      medPlan[med][9] = interval
    }//end if the same medication and the same resident

  }//end for medPlan

但它也不断更改重复任务的第一行,即使日志显示它确定了正确的行。

当前结果:

非常感谢任何帮助!

引用问题。必须添加 .slice(0) 才能解决问题。

medPlan.push(medPlan[medPlan.length-1].slice(0));