Google Apps脚本触发器-每当将新文件添加到文件夹时运行 [英] Google Apps Script trigger - run whenever a new file is added to a folder

查看:46
本文介绍了Google Apps脚本触发器-每当将新文件添加到文件夹时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当将 new 文件添加到特定文件夹时,我都希望执行google apps脚本.

I want to execute a google apps script whenever a new file is added to a specific folder.

当前,我使用的是每x分钟运行一次的时钟触发器,但是只要将文件添加到文件夹中,我就只需要运行该脚本.有办法吗?

Currently I'm using a run-every-x-minutes clock trigger, but I only need to run the script whenever I add a file to a folder. Is there a way to do this?

问题-现在已经快3年了.问题下方的评论指出:

The same as this question - which is now almost 3 years old. The comment below the question states that:

如果您希望这样做,则没有触发条件.如何东西进入文件夹,您对此有任何控制权吗?–杰西·谢勒(Jesse Scherer)'18年4月8日,下午3:02

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

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

I wonder if this comment is still valid, and if it is, then if there's a workaround.

推荐答案

问题:

很遗憾,您阅读的评论仍然为 true .此处是所有可用触发器和添加到文件夹的新文件不是其中的一个.

Issue:

Unfortunately, the comment you read is still true. Here is a list of all the available triggers and a trigger for a new file added to a folder is not one of them.

我可以为您提供一种解决方法,开发人员通常在构建其附件时会使用这些解决方法.您可以利用 PropertiesService 类.逻辑很简单.

I can offer you a workaround which is usually used by developers when they built their add-ons. You can take advantage of the PropertiesService class. The logic is quite simple.

  1. 您将存储范围为脚本的键/值对:

在您的情况下,键将是文件夹ID,值将是此文件夹下的文件数.

In your case, the key will be the folder id, and the value will be the number of files under this folder.

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

  1. You will setup a time-driven trigger to execute mainFunction for example every one minute.

该脚本将计算所选文件夹中的当前文件数.负责此功能的是 countFiles .

The script will count the current number of files within the selected folder. The function responsible for that is countFiles.

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

The checkProperty function is responsible for checking if the current number of files under this folder matches the old number of files. If there is a match, meaning no files were added, then checkProperty returns false, otherwise return true and update the property for the current folder ID, so when the script runs after 1 minute, it will compare with the fresh value.

如果 checkProperty 返回 true ,则执行所需的代码.

If checkProperty returns true, then execute the desired code.

代码段:

mainFunction 设置时间驱动的触发器.如果 folderID 下的文件数已更改,则将执行您放置在 if(runCode)语句括号内的任何代码.

Code snippet:

Set up a time-driven trigger for mainFunction. Whatever code you put inside the brackets of the if(runCode) statement will be executed if the number of files under the folderID has changed.

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!");
   //
  }
}

这是需要在同一项目中的帮助程序功能(您可以将它们放在相同的脚本或不同的脚本中,但可以在相同的脚本编辑器"中).

And here are the helper functions which need to be in the same project (you can put them in the same script or different scripts but in the same "script editor").

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;
  }
}

这篇关于Google Apps脚本触发器-每当将新文件添加到文件夹时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆