javascript - 谷歌应用程序脚本 : How to properly work with comma separated spreadsheet values converted to an array

标签 javascript arrays json google-apps-script

我有一个电子表格,其中第 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(", ")。这样可以容纳空间并消除我的问题。

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

关于javascript - 谷歌应用程序脚本 : How to properly work with comma separated spreadsheet values converted to an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58526916/

相关文章:

java - 如何使用 Google 的 Gson 从 JSON 响应中获取特定值?

javascript - 带动态列的 JQGrid : cellattr does not work

javascript - Webstorm Karma 测试运行程序无法与 Yeoman Angular-fullstack 一起使用

arrays - 当容量减少时, slice 的行为如何?

javascript - 无法使用 Ajax 将数据列表从 Controller 返回到 View 中的下拉列表。我收到未定义的未定义错误

javascript - 当我添加removeMarker函数时,每5秒在 map 上加载图钉不显示图标

javascript - 将两个对象与多个 Node 组合起来 - 为什么这段代码可以工作?

javascript - IE8 远程页面渲染错误

arrays - 给定大小为 N 的数组的三个数字的最大乘积

javascript - 在javascript中将数组值转换为字符串