getRange getValues和来自特定列的长度for循环停在正确的行

时间:2016-09-08 20:42:21

标签: google-apps-script google-sheets

我创建了一个循环,但我无法在正确的行停止。 在我的电子表格中,当公式符合某些条件时,我的公式显示为空(“”),但.length仍然将这些单元格标识为已满数据(尽管单元格中未显示任何数据),因此循环运行的时间比需要的长。

我要么.length要么不识别没有显示数据的单元格,要么我想根据我知道没有这样的公式的特定列来设置循环的.length正在创建一个更大的.length

如果我使用以下代码,它也会使用公式读取单元格,因此运行时间过长。

   function makesCopiesOfTemplate_renamesTheCopies_PastesUrlAndIds() {
      var ssDataMadre = SpreadsheetApp.openById("1d6lw9R4WaPUCyHWcBRr8xDyZFjBNvUMH6BE_U2NXB8Q"); // PLANILLA DATA MADRE
      var sheetDataMadre = ssDataMadre.getSheets();
      var rangeDataMadre = sheetDataMadre[0].getDataRange();
      var valuesDataMadre = rangeDataMadre.getValues();

    // FOR loops STARTS here
      for (i=4;i<valuesDataMadre.length;i++) {
      namesPlanillas = valuesDataMadre[i][13];
      canEdit1 = valuesDataMadre[i][17];
      canEdit2 = valuesDataMadre[i][18];
      canEdit3 = valuesDataMadre[i][19];
      canComment1 = valuesDataMadre[i][20];
      canComment2 = valuesDataMadre[i][21];
      canComment3 = valuesDataMadre[i][22];
// Making a "copy of" the correct spreadsheet  
  var ssMateriaMadre = SpreadsheetApp.openById(urlTemplateMateria); // Activates "PLANILLA MATERIA MADRE"
  var sheetMateriaMadre = SpreadsheetApp.getActiveSheet(); // Activates de Sheet within the above mentioned spreadsheet
  var destFolderForCopy = DriveApp.getFolderById(destFolderIDmaterias); //Identifies de destination folder, from Folder ID in the spreadsheet.
  var identifyFileToCopy = DriveApp.getFileById(ssMateriaMadre.getId()) // Identifies the file that has been activated above
  var copiedFile = identifyFileToCopy.makeCopy("GAS9_"+namesPlanillas, destFolderForCopy); // (1) Makes a copy of identified file. (2) Defines the name I want (3) Puts it in the correct folder.

/*  FUNCIONA, SOLO LO OCULTO PARA LAS PRUEBAS...
// Add Editors, Commenters & Viewers to the file. 
  var editFile = copiedFile.addEditor(canEdit1)
  var editFile = copiedFile.addEditor(canEdit2)
  var editFile = copiedFile.addEditor(canEdit3)
  var commentFile = copiedFile.addCommenter(canComment1)
  var commentFile = copiedFile.addCommenter(canComment2)
  var commentFile = copiedFile.addCommenter(canComment3)
  var viewFile = copiedFile.addViewer(canView1)
  var viewFile = copiedFile.addViewer(canView2)
  var viewFile = copiedFile.addViewer(canView3)
*/

// Get the URL of the CopiedFile (defined in "copiedFile" variable).
  var url = copiedFile.getUrl();
// Get the KEY of the CopiedFile (defined in "copiedFile" variable).
  var key = copiedFile.getId();

// Pastes the URL of the CopiedFile into column 7 of the 
  var pasteUrl = sheetDataMadre[0].getRange(i+1, 16);
  // SetValue escribe lo que le digamos, en el "range" que definimos arriba...
  pasteUrl.setValue(url);

// Copiar el KEY/ID del nuevo documento en la columna 8
  var pasteId = sheetDataMadre[0].getRange(i+1, 17);
  // SetValue escribe lo que le digamos, en el "range" que definimos arriba...
  pasteId.setValue(key);
  }
// FOR loop ENDS here
}

这是我尝试根据不同的变量定义长度。此方法仍未按预期执行,似乎获得整张表的.length

    function makesCopiesOfTemplate_renamesTheCopies_PastesUrlAndIds() {
      var ssDataMadre = SpreadsheetApp.openById("1d6lw9R4WaPUCyHWcBRr8xDyZFjBNvUMH6BE_U2NXB8Q"); // PLANILLA DATA MADRE
      var sheetDataMadre = ssDataMadre.getSheets();
      var rangeDataMadre = sheetDataMadre[0].getDataRange();
      var valuesDataMadre = rangeDataMadre.getValues();

//new variable trying to just get the values of COLUMN  B
      var valuesColumna_B_Materia = sheetDataMadre[0].getRange("B5:B2000").getValues(); 

     // FOR loops STARTS here
          for (i=4;i<valuesColumna_B_Materia.length;i++) {
          namesPlanillas = valuesDataMadre[i][13];
          canEdit1 = valuesDataMadre[i][17];
          canEdit2 = valuesDataMadre[i][18];
          canEdit3 = valuesDataMadre[i][19];
          canComment1 = valuesDataMadre[i][20];
          canComment2 = valuesDataMadre[i][21];
          canComment3 = valuesDataMadre[i][22];
// Making a "copy of" the correct spreadsheet  
  var ssMateriaMadre = SpreadsheetApp.openById(urlTemplateMateria); // Activates "PLANILLA MATERIA MADRE"
  var sheetMateriaMadre = SpreadsheetApp.getActiveSheet(); // Activates de Sheet within the above mentioned spreadsheet
  var destFolderForCopy = DriveApp.getFolderById(destFolderIDmaterias); //Identifies de destination folder, from Folder ID in the spreadsheet.
  var identifyFileToCopy = DriveApp.getFileById(ssMateriaMadre.getId()) // Identifies the file that has been activated above
  var copiedFile = identifyFileToCopy.makeCopy("GAS9_"+namesPlanillas, destFolderForCopy); // (1) Makes a copy of identified file. (2) Defines the name I want (3) Puts it in the correct folder.

/*  FUNCIONA, SOLO LO OCULTO PARA LAS PRUEBAS...
// Add Editors, Commenters & Viewers to the file. 
  var editFile = copiedFile.addEditor(canEdit1)
  var editFile = copiedFile.addEditor(canEdit2)
  var editFile = copiedFile.addEditor(canEdit3)
  var commentFile = copiedFile.addCommenter(canComment1)
  var commentFile = copiedFile.addCommenter(canComment2)
  var commentFile = copiedFile.addCommenter(canComment3)
  var viewFile = copiedFile.addViewer(canView1)
  var viewFile = copiedFile.addViewer(canView2)
  var viewFile = copiedFile.addViewer(canView3)
*/

// Get the URL of the CopiedFile (defined in "copiedFile" variable).
  var url = copiedFile.getUrl();
// Get the KEY of the CopiedFile (defined in "copiedFile" variable).
  var key = copiedFile.getId();

// Pastes the URL of the CopiedFile into column 7 of the 
  var pasteUrl = sheetDataMadre[0].getRange(i+1, 16);
  // SetValue escribe lo que le digamos, en el "range" que definimos arriba...
  pasteUrl.setValue(url);

// Copiar el KEY/ID del nuevo documento en la columna 8
  var pasteId = sheetDataMadre[0].getRange(i+1, 17);
  // SetValue escribe lo que le digamos, en el "range" que definimos arriba...
  pasteId.setValue(key);
  }
// FOR loop ENDS here
}

1 个答案:

答案 0 :(得分:0)

我遇到过类似的问题。问题来自您定义范围的位置。当您告诉代码查找数据范围时,包括公式。我建议使用Logger.log()来查看您创建的.getValues数组。这将有助于了解如何定义您想要停止的位置。

作为一种潜在的解决方案,如果存在行数据,则需要引用始终包含数据的列,以及不使用公式。表单提交的时间戳是一个很好的起点。您还可以在其中一列上使用条件格式(用于背景,字体颜色,边框等),并让代码查找已格式化的行,而不是它们是否有数据。

例如,如果行包含数据,请将此单元格设置为绿色。告诉你的代码循环遍历该列,如果为绿色,则执行代码。如果没有,continue或停止。这将为您提供更准确的数据范围,并允许您更精确地控制代码运行的时间。