本文介绍了如何将Google Appscript应用于所有Google文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下解决方案旨在生成自动增量ID,以替换googledocs的默认名称未倾斜文档".

Below solution is intended to generate auto-increment ID as replacement to default name "Untitiled document" of googledocs.

Google文档中的自动增量文件名?

解决方案对我来说很清楚.但是,我正在考虑如何将此脚本应用于将即时创建的所有google文档的选项?也许附加组件?

The solution was clear to me. However, I'm thinking an option on how to apply this script to all google docs that will be created on the fly ? Maybe add-ons ?

推荐答案

答案:

自动重新命名文档无法即时完成,也就是说,无法使脚本在创建文档时自动运行.不过,您可以在云端硬盘中检查新文档,然后使用重复计时器上的脚本将其重命名.

根据文档,就简单的触发器而言,

As far as simple triggers go, according to the documentation:

所以这是不行的.但是,可安装的触发器可以作为独立项目运行,也可以在基于时间的触发器上运行.可以将可安装触发器设置为的重复间隔的最短时间范围是 每分钟一次 .我希望这适合您的需求.

So this is a no-go. Installable triggers however can be run as a standalone project, and can be run on a time-based trigger. The shortest time frame for the recurring interval an installable trigger can be set to is once a minute. I hope this is suitable for your needs.

因此,您要做的是检查驱动器中最近一次创建的文档,然后根据您的增量值对其进行重命名.

What you can therefore do, is check your Drive for Documents that have been created in the latest interval, and then rename them based on your increment value.

修改TheMaster在您链接的问题中提供的精彩代码:

Modifying the wonderful code provided by TheMaster in the question you linked:

function renameFiles() {
  // change this to be your file name prefix
  const fileNameStarter = "init";
  var dict = {};
  var now = new Date();
  var newDate = new Date(now.getTime() - 60000);
  // The date search has to be in UTC so we modify the time to be in the
  // correct format
  var searchString = Utilities.formatDate(newDate, "UTC", "yyyy-MM-dd'T'HH:mm:ss");

  // Get a list of newly modified Drive files
  var newFiles = DriveApp.searchFiles('modifiedDate > "' + searchString + '"');
  var arr = [];

  while (newFiles.hasNext()) {
    var currFile = newFiles.next();

    // check if the file is a) a Google Doc and b) already named correctly
    if (currFile.getMimeType() == MimeType.GOOGLE_DOCS && !currFile.getName().startsWith(fileNameStarter)) {
      dict[new Date(currFile.getDateCreated())] = currFile.getId();
      arr.push(new Date(currFile.getDateCreated()));
    }
  }
  // sorts the files not named correctly by date
  arr.sort()

  const correctlyNamedFiles = DriveApp.searchFiles("title contains '" + fileNameStarter + "' and mimeType = 'application/vnd.google-apps.document'");
  var count = 0;
  // gets the number of already named files so to get the new number
  while (correctlyNamedFiles.hasNext()) {
    count++;
    correctlyNamedFiles.next()
  }

  // get the new files to rename, and rename them
  for (var j = 0; j < arr.length; j++) {
    count++
    var formatNum = ('00000' + count).substr(-4);
    DriveApp.getFileById(dict[arr[j]]).setName(fileNameStarter + formatNum)
  }
}

请记住, modifiedDate文件查询项默认为UTC.

Remember that the modifiedDate file query term defaults to UTC if the timezone offset isn't specified.

使用保存图标保存脚本,按运行按钮(►),并确认运行脚本的身份验证.

Save the script with the save icon, press the run button (►), and confirm the authentication of running the script.

从此处开始,按照编辑">当前项目的触发器菜单项,您将在G Suite开发人员中心中打开一个新页面.点击右下角的+添加触发器按钮,然后按以下步骤设置触发器设置:

From here, following the Edit > Current project's triggers menu item, you will have a new page open in the G Suite Developer Hub. Click the + Add Trigger button in the bottom right and set up the trigger settings as follows:

然后按保存.现在,此操作将每分钟运行一次,并使用initXXXX格式重命名所有新修改的Document文件,除非已正确命名该文件.您可以通过编辑fileNameStarter变量将前缀更改为所需的前缀.

And press save. This will now run once a minute, renaming all newly modified Document files with the initXXXX format, unless the file is already correctly named. You can change the prefix to what you like by editing the fileNameStarter variable.

希望对您有帮助!

  • Installable Triggers | Apps Script | Google Developers
  • Search query terms | Google Drive API | Google Developers
  • G Suite and Drive MIME Types
  • Method formatDate(date, timeZone, format | Class Utilities

这篇关于如何将Google Appscript应用于所有Google文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 13:02