1

The newFile() method of the Advanced Drive API Service seems to return an object, but not create a file in my Google Drive. I can't find any documentation for this. Here is code that I tried, and the results I got.

function makeNewDoc() { 

  var file = Drive.newFile();
  Logger.log('file ' + file);
  
  file = {one: "value One"};
  Logger.log('file ' + file);
  
  Logger.log("file['one'] " + file['one']);
};

The LOGS print this:

file {}

file [object Object]

file['one'] value One

I tried adding a string inside the newFile() parenthesis:

function makeNewDoc() { 

  var file = Drive.newFile("some text");
  Logger.log('file ' + file);
  
  file = {one: "value One"};
  Logger.log('file ' + file);
  
  Logger.log("file['one'] " + file['one']);
  
  for (var key in file) {
     Logger.log('key: ' + key);
     Logger.log('value: ' + file[key]);
  };
};

That didn't produce an error, but it doesn't do anything either. Or I don't know what it's doing.

I've checked my Google drive many times after running the code many times, and there is never a new file in my Drive.

The newFile() method returns an object, that I can put new properties into, and then enumerate. But other than that, I have no idea what it does, or what use it has.

So, if anyone can tell me what the newFile() method, of the Drive API does, I would really like to know.

Community
  • 1
  • 1
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • 1
    Drive.Files.insert() is what you're looking for – pinoyyid Feb 10 '15 at 05:56
  • Thanks. I did see an example of how to "upload" an image file with `insert()`. But I couldn't find a way to create a mime type of `Google_Doc`. That's probably a different question though. – Alan Wells Feb 10 '15 at 15:09

2 Answers2

5

Below is an example how to make a new document with advanced Drive service

function createNewDoc(title,content,folderId){

  var content = content || "";

  // neither kind or mimeType properties seem to be necessary
  // for Doc to be created, but are being included anyhow 
  var resource = {
    title: title,
    parents: [
      {
        "id": folderId,
        "kind": "drive#fileLink"
      }
    ],
    mimeType: 'application/vnd.google-apps.document'
  };

  var blob = Utilities.newBlob(content);
  var newfile = Drive.Files.insert(resource, blob, {"convert":"true"});

  return newFile;
}
Bryan P
  • 5,031
  • 3
  • 30
  • 44
Spencer Easton
  • 5,642
  • 1
  • 16
  • 25
  • Thank you. I up-voted your answer, but I'm getting some errors. I posted a new Question: [Stack Overflow Question](http://stackoverflow.com/questions/28436234/apps-script-advanced-drive-api-create-google-doc-type-mime-type-error) – Alan Wells Feb 10 '15 at 16:06
  • Did either of you ever get `content` to take on any formatting or be assigned any `DocumentApp` element types ( heading, list, ect )? It gets added as Paragraph type and I can get separate paragraphs with spacing, but haven't seen much outside of that. – Bryan P Jan 29 '16 at 10:14
  • Never mind, looks like changing mimeType to 'text/html' and moving it back into the blob as a second parameter allows content to show up with html styling – Bryan P Jan 29 '16 at 11:08
1

I used the answer code and there was a problem when creating the blob as HTML. The below code converts HTML as a string to a blob to be inserted into a document.

  var content = '<!DOCTYPE html><html><body><b>Hello</b></body><html>'


  var blob = Utilities.newBlob("").setDataFromString(content,'UTF-8').setContentType("text/html")
  var newfile = Drive.Files.insert(resource, blob, {"convert":"true"});
Jason Allshorn
  • 1,625
  • 1
  • 18
  • 27