How can I get the folder ID of a folder in my drive using google script?

ARC UNSTUMPABLE

For a project I am working on I created a trigger to run my makeCopy() function at the beginning of every year. The goal is to make a copy of a spreadsheet and save it in a folder already created in my drive. In this function I also create a new folder for the new year.

function makeCopy(){

  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var d = new Date();
  var year = d.getYear();
  var FolderName = "Time Off Request - " + year;

  DriveApp.createFolder(FolderName);

  year = year - 1;
  var FileName = "Copy of Time Off Request - " + year; 
  Logger.log(FolderName);
  FolderName = "Time Off Request - " + year;
  var FolderID = DriveApp.getFoldersByName(FolderName).next().getId();
  Logger.log(FolderID);

  var destFolder = DriveApp.getFolderById(FolderID);
  Logger.log(destFolder);


  DriveApp.getFileById(sheet.getId()).makeCopy(FileName, destFolder);

}

This code actually ran and worked the first time. However, every time I try to test it again I get the error, "Cannot retrieve the next object: iterator has reached the end. " On the line where I try to get my folder ID. How can I fix this error and get the folder ID of the folder in my drive so I can make a copy of the spreadsheet to that folder.

tehhowch

You get that specific error, Cannot retrieve the next object: iterator has reached the end., because the search for that folder name yielded an empty result set. If you alter your program structure to only use next() if the iterator's hasNext() method returns true, you will not get that error anymore. However, such a change will not make your search term suddenly find results. It does, however, allow for making repeated attempts with different search queries.

I suggest renaming your variables to be expressive of their intent:

// Copy the current file into last year's folder, and create this year's folder.
function makeCopy() {
  var now = new Date();
  var thisYear = now.getYear();
  var lastYear = thisYear - 1;

  var query = "Time Off Request - " + lastYear;
  var folderIterator = DriveApp.getFoldersByName(query);
  // Instead of throwing an error, you could modify this to be a while loop
  // that requests a search term from the user - see Browser/UI and inputBox
  // and updates folderIterator. If you do that, you'll want to add guards
  // that ensure the correct folder will be the first result.
  if (!folderIterator.hasNext()) {
    throw new Error("Bad search term - no folders with name '" + query + "' exist!");
  }

  var lastYearFolder = folderIterator.next();
  console.log({message: "Backup of Time Off Requests",
               query: query,
               destFolder: lastYearFolder.getName()}
  ); // View->Stackdriver Logging

  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var backupName = "Copy of Time Off Request - " + lastYear; 
  DriveApp.getFileById(sheet.getId()).makeCopy(backupName, lastYearFolder);

  // Before exiting, now that the backup is done, create this year's folder.
  DriveApp.createFolder("Time Off Request - " + thisYear);
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to get folder list from google drive?

分類Dev

How can I find out which program creates a file/folder in my C: drive?

分類Dev

How can I keep a folder synchronized to an external USB hard drive?

分類Dev

How to run a script from a mounted Google drive. The issue is that the google drive folder has a space in its name

分類Dev

Google DRIVE API V3 - Get a root folder ID with nodejs

分類Dev

How can I get the revision history in Google Docs using a script?

分類Dev

Python Script Creating Binary File Instead of Folder in Google Drive

分類Dev

How can I get a specific folder with a specific folders sibling in python

分類Dev

Can I replace a program folder with a symbolic link to another drive?

分類Dev

How can I copy a folder to a new folder renamed with the sysdate?

分類Dev

How to access Google Drive appdata folder file with Flutter?

分類Dev

How do I find which external drive a folder is mounted from?

分類Dev

How I can make a button for browse a folder?

分類Dev

Trying to get folder path of file in Google Apps Script

分類Dev

Google apps script: Get folder name from file

分類Dev

Mounting a physically separate drive as my home folder?

分類Dev

How can I change the default location of content directories (eg Pictures, Templates, Music) in my home folder?

分類Dev

Project tab stucked on loading screen How can I see my project folder on left?

分類Dev

How can I prevent Windows 10 from stretching and cropping my folder thumbnails?

分類Dev

How can I run command in a folder without changing my current directory to it?

分類Dev

How can I run command in a folder without changing my current directory to it?

分類Dev

Can I unhide a single folder or file with batch script?

分類Dev

google drive listing files in public shared folder

分類Dev

ASP.NET MVC How may I register a resource file in my view folder, to be used as @using

分類Dev

How can I get duration of all video files in a folder containing multiple subfolders?

分類Dev

How can I access a shared folder from R on Ubuntu to a 2nd Ubuntu machine using Samba?

分類Dev

By using below code how can i delete image from folder in asp .net

分類Dev

How does I get image store in folder using path Store in sql

分類Dev

How can I process a form using Google Apps Script?

Related 関連記事

  1. 1

    How to get folder list from google drive?

  2. 2

    How can I find out which program creates a file/folder in my C: drive?

  3. 3

    How can I keep a folder synchronized to an external USB hard drive?

  4. 4

    How to run a script from a mounted Google drive. The issue is that the google drive folder has a space in its name

  5. 5

    Google DRIVE API V3 - Get a root folder ID with nodejs

  6. 6

    How can I get the revision history in Google Docs using a script?

  7. 7

    Python Script Creating Binary File Instead of Folder in Google Drive

  8. 8

    How can I get a specific folder with a specific folders sibling in python

  9. 9

    Can I replace a program folder with a symbolic link to another drive?

  10. 10

    How can I copy a folder to a new folder renamed with the sysdate?

  11. 11

    How to access Google Drive appdata folder file with Flutter?

  12. 12

    How do I find which external drive a folder is mounted from?

  13. 13

    How I can make a button for browse a folder?

  14. 14

    Trying to get folder path of file in Google Apps Script

  15. 15

    Google apps script: Get folder name from file

  16. 16

    Mounting a physically separate drive as my home folder?

  17. 17

    How can I change the default location of content directories (eg Pictures, Templates, Music) in my home folder?

  18. 18

    Project tab stucked on loading screen How can I see my project folder on left?

  19. 19

    How can I prevent Windows 10 from stretching and cropping my folder thumbnails?

  20. 20

    How can I run command in a folder without changing my current directory to it?

  21. 21

    How can I run command in a folder without changing my current directory to it?

  22. 22

    Can I unhide a single folder or file with batch script?

  23. 23

    google drive listing files in public shared folder

  24. 24

    ASP.NET MVC How may I register a resource file in my view folder, to be used as @using

  25. 25

    How can I get duration of all video files in a folder containing multiple subfolders?

  26. 26

    How can I access a shared folder from R on Ubuntu to a 2nd Ubuntu machine using Samba?

  27. 27

    By using below code how can i delete image from folder in asp .net

  28. 28

    How does I get image store in folder using path Store in sql

  29. 29

    How can I process a form using Google Apps Script?

ホットタグ

アーカイブ