How to apply google appscript to all google docs

泪湿孤枕 提交于 2020-07-22 06:10:46

问题


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

Autoincrement filename in Google Docs?

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 ?


回答1:


Answer:

Auto-renaming documents can't be done on-the-fly, that is to say, there is no way of making a script run automatically on document creation. You can however check for new documents in the Drive and rename them using a script which is on a repeating timer.

More Information:

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

The script must be bound to a Google Sheets, Slides, Docs, or Forms file, or else be an add-on that extends one of those applications.

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.

Code:

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

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

Creating an Installable Trigger:

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

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:

  • Choose which function to run: renameFiles
  • Choose which deployment should run: Head
  • Select event source: Time-driven
  • Select type of time based trigger: Minutes timer
  • Select minute interval: Every minute

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.

I hope this is helpful to you!

References:

  • 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

Related Questions:

  • Autoincrement filename in Google Docs?


来源:https://stackoverflow.com/questions/60825332/how-to-apply-google-appscript-to-all-google-docs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!