Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File iterator in Google Apps script is returning an invalid argument when searching for a file that doesn't exist

I'm trying to copy mail content with a label (read from a google sheet) into a document with the subject as the doc name. If a doc already exists, I need to add mail body in the same doc, if not, I need to create a doc with the subject as doc name. part of the script that I'm using is this:

function searchdrive(x) {
  var iterator = DriveApp.searchFiles(x);

  if(iterator.hasnext()) {  
    var docid = iterator.next().getId();
    return docid;
    Logger.log(docid);
   }
  var doc = DocumentApp.create(x);
  docid = doc.getId();

  return docid;
 }

If a doc is not already there, iterator.hasnext() should be returning false but instead it's throwing an error as invalid argument.

How do I work around with this?

like image 716
Victor von Doom Avatar asked Oct 23 '25 15:10

Victor von Doom


2 Answers

Googles Apps Scripts utilizes version2 of the Drive API, not v3.

Substitute 'title' for 'name' in your query string.

v2 Drive API: Search Parameters documentation

like image 94
remyActual Avatar answered Oct 26 '25 05:10

remyActual


There are some rules for an argument passed to DriveApp.searchFiles(params) read this documentation https://developers.google.com/apps-script/reference/drive/drive-app#searchfilesparams

like image 41
Nitin Dhomse Avatar answered Oct 26 '25 05:10

Nitin Dhomse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!