Apps Script Advanced Drive API Service - newFile&#

2019-08-14 00:25发布

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.

2条回答
相关推荐>>
2楼-- · 2019-08-14 00:53

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;
}
查看更多
再贱就再见
3楼-- · 2019-08-14 00:59

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"});
查看更多
登录 后发表回答