Victor von Doom
Victor von Doom

Reputation: 53

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?

Upvotes: 4

Views: 2857

Answers (2)

remy-actual
remy-actual

Reputation: 824

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

Upvotes: 4

Nitin Dhomse
Nitin Dhomse

Reputation: 2612

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

Upvotes: 1

Related Questions