繁体   English   中英

谷歌应用脚本:如何正确使用逗号分隔的电子表格值转换为数组

[英]Google apps scripts: How to properly work with comma separated spreadsheet values converted to an array

我有一个电子表格,其中第 1 列包含源文件 ID; 每个单元格只包含一个 ID。 在第 2 列中,存在目标文件 ID,其中单个单元格包含多个用逗号分隔的 ID。 我有一个脚本可以获取这些值并执行其他操作。 这是一个片段,其中包括对相关变量所做的所有操作:

// Retrieve the source and destination IDs from spreadsheet
  var toFileIds = myFile.getRange(2,2,key.getLastRow()-1, 1).getValues();               // Get the destination file IDs from myFile
  var sourceFileIds = myFile.getRange(2,1,key.getLastRow()-1, 1).getValues();           // Get the source file IDs from myFile

// Write-out the global vars 
  var stringtoFileIds = JSON.stringify(toFileIds);                                       // Convert to string as required by PropertiesService
  var stringsourceFileIds = JSON.stringify(sourceFileIds);                               // Convert to string as required by PropertiesService
  PropertiesService.getScriptProperties().setProperty('toFileIds', stringtoFileIds);    
  PropertiesService.getScriptProperties().setProperty('sourceFileIds', stringsourceFileIds);  

我有第二个脚本,它查看源文件并找到上面标识的相应目标文件:

// Read-in global vars
  toFileIds = PropertiesService.getScriptProperties().getProperty('toFileIds');
  sourceFileIds = PropertiesService.getScriptProperties().getProperty('sourceFileIds');
  toFileIds = JSON.parse(toFileIds);
  sourceFileIds = JSON.parse(sourceFileIds);

// Retrieve the destination file IDs  
  var ArrPos = sourceFileIds.indexOf('activeFile'); // Find the position of the active file name within the stored arr   
  var destFileIds = destFileIds[ArrPos]; // Use that position to find the destination files

  var destFileIds = destFileIds.toString().split(","); // Do this so that each comma separated ID is its own element in arr


// Mark if there are no destination files  
  if (destFileIds == undefined) {
    var destFileIds = "no";
  }  

// Cache the var
  cache = CacheService.getPublicCache();
  cache.put("destFileIds", JSON.stringify(destFileIds), 400); 

最后,我有第三个脚本,其中包含一个错误检查片段。 问题就在这里:

if (destFileIds != "no") {
  for (var i = 0; i <= destFileIds.length -1; i++) {
    try {
      DriveApp.getFileById(destFileIds[i]); 
    } catch (e) {
      //Logger.log(e.message);
      throw 'At least one of the destination files does not exist.';
    }
  }
}

对于这个最终的代码片段,我希望仅当数组中的 ID 不存在时才会出现错误消息。 destFileIds的所有元素都存在时,此代码段适用于第一个元素,但随后为数组的第二个元素引发上述定义的错误并随后退出。 我 100% 确定所有文件 ID 都是正确的。 我通过将它们放在 URL 中手动检查了这一点。 我还手动定义了一个具有这些 ID 的数组,并且代码运行良好,即按预期运行:

var destFileIds = ["myID1", "myID2", "myID3"]; // Where these are replaced with real IDs

if (destFileIds != "no") {
  for (var i = 0; i <= destFileIds.length -1; i++) {
    try {
      DriveApp.getFileById(destFileIds[i]);
    } catch (e) {
      //Logger.log(e.message);
      throw 'No updates were made because all of the related (copy to) templates do not appear to exist.';
    }
  }
} 

关于为什么第二个元素的读取可能与destFileIds的第一个元素不同的任何想法?

这是一个简单的错误。 我的电子表格单元格中的目标文件不仅仅由逗号分隔。 它们用逗号隔开,后跟一个空格。 我将var destFileIds = destFileIds.toString().split(",")更改为var destFileIds = destFileIds.toString().split(", ") 这可以容纳空间并消除我的问题。

希望这可以防止其他人像我一样在这方面浪费太多时间……

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM