Google Apps 脚本触发器 - 运行 每当将新文件添加到文件夹时

Google Apps Script trigger - run whenever a new file is added to a folder

我想在将 文件添加到特定文件夹时执行 google 应用程序脚本。

目前我正在使用 运行-每 x 分钟的时钟触发器,但只要我将文件添加到文件夹,我只需要 运行 脚本。有办法吗?

this 问题相同 - 现在已经快 3 岁了。问题下方的评论指出:

There's not a trigger for that, if that's what you're hoping. How are things getting into the folder, and do you have any control over that? – Jesse Scherer Apr 8 '18 at 3:02

我想知道这个评论是否仍然有效,如果是,那么是否有解决方法。

问题:

很遗憾,您阅读的评论仍然是真实Here 是所有可用触发器的列表,new file added to a folder 的触发器是 而不是 其中之一。

解决方法/解释:

我可以为您提供一种变通方法,开发人员在构建附加组件时通常会使用这种变通方法。你可以利用PropertiesServiceclass。道理很简单。

  1. 您将存储作用域为脚本的键值对:

在您的情况下,键是文件夹 ID,值是该文件夹下的文件数。

  1. 您将设置一个 时间驱动的 触发器以每隔一分钟执行一次 mainFunction

  2. 该脚本将计算所选文件夹中的当前文件数。负责的函数是 countFiles.

  3. checkProperty函数负责检查此文件夹下的当前文件数是否与旧文件数匹配。如果匹配,意味着没有添加文件,则 checkProperty returns false,否则 return true 并更新 属性当前文件夹 ID,因此当脚本在 1 分钟后运行时,它将与新值进行比较。

  4. 如果checkPropertyreturnstrue,则执行需要的代码

代码片段:

mainFunction 设置一个时间驱动的触发器。如果 folderID 下的文件数发生变化,则将执行 if(runCode) 语句括号内的任何代码。

function mainFunction(){
  const folderID = 'folderID'; //provide here the ID of the folder
  const newCounter = countFiles(folderID);
  const runCode = checkProperty(folderID, newCounter);
  
  if(runCode){
   // here execute your main code
   // 
    console.log("I am executed!");
   //
  }
}

这里是需要在同一个项目中的辅助函数(你可以将它们放在同一个脚本或不同的脚本中,但在同一个“脚本编辑器”中)。

function countFiles(folderID) {
  const theFolder = DriveApp.getFolderById(folderID);
  const files = theFolder.getFiles();
  let count = 0;
  while (files.hasNext()) {
   let file = files.next();
   count++;
   };
  return count;
}


function checkProperty(folderID, newC){
  const scriptProperties = PropertiesService.getScriptProperties();
  const oldCounter = scriptProperties.getProperty(folderID);
  const newCounter = newC.toString();
  if(oldCounter){
    if(oldCounter==newCounter){
      return false;
    }
    else{
      scriptProperties.setProperty(folderID, newCounter);  
      return true;
    }
  }
  else{
     scriptProperties.setProperty(folderID, newCounter);  
     return true;
  }
}