DocumentApp.openById() fails with “Service unavail

2019-02-21 04:53发布

I'm sure I must be doing something silly, but I'm not seeing what it is. How can I get the contents of a simple text file? Does it need a .txt extension perhaps? Perhaps it has something to do with how the file was created over a year ago?

The error was: 'Service unavailable: Docs (line 61, file "fileFuncs")'. Line 61 is highlighted below.

Thanks in advance for your help.

function testGetFile()
{

getFile("testGetFile", "WhoAmI");

}

function getFile(calledFrom, targetFile)
{

var root = DriveApp.getRootFolder();
var files=root.getFiles();
var numFiles=0;

 while (files.hasNext()) {
   var file = files.next();
   numFiles++;
   var filename = file.getName();


    if (filename == targetFile) {

      Logger.log("Success.  Found file: " + filename);
      Logger.log(' Number of files processed = '+numFiles+'\n\n');

      var fileId = file.getId();
      **var doc = DocumentApp.openById(fileId);       // Line 61:THIS FAILS with above error**
      var body=doc.getBody();
      var text=body.getText();

      Logger.log('text = '+text);// there it is         

      return text;
    }   

   } // while()

return -1;
}

2条回答
Luminary・发光体
2楼-- · 2019-02-21 05:07

Now that we know it's a text/plain doc we can add the good method to handle it. As Serge said in it's post the method you are using is only valid for "google document" files so you must check that's a google document file before using it. We are going to do the same but with text/plain documents:

function testGetFile(){
  getFile("testGetFile", "WhoAmI");
}

function getFile(calledFrom, targetFile){
  var root = DriveApp.getRootFolder();
  var files=root.getFiles();

  /*
  instead of a root getFiles() I prefer to use searchFiles
  I'ts less work for your script.
  the documentation on search: https://developers.google.com/drive/web/search-parameters
  */
  files = root.searchFiles("title = 'WhoAmI' and mimeType = 'text/plain'"); // if it's a txt doc it should have .txt at the end of it's title: "WhoAmI.txt"
  var numFiles=0;

  while (files.hasNext()) {
    var file = files.next();
    numFiles++;
    var filename = file.getName();
    var mime = file.getMimeType();
    Logger.log(mime); // produce a lot of output

    if (filename == targetFile && mime=="application/vnd.google-apps.document") {

      Logger.log("Success.  Found GOOGLE DOC file: " + filename);
      Logger.log(' Number of files processed = '+numFiles+'\n\n');

      var fileId = file.getId();
      var doc = DocumentApp.openById(fileId);       // this won't fail anymore (I hope)
      var body=doc.getBody();
      var text=body.getText();

      Logger.log('text = '+text);// there it is         

      return text;
    }
    else if(filename == targetFile && mime=="text/plain") {
      Logger.log("Success.  Found TXT file: " + filename);
      Logger.log(' Number of files processed = '+numFiles+'\n\n');
      var text = file.getBlob().getDataAsString();
      Logger.log('text = '+text);// there it is         

      return text;
    }
  } // while()  
  return -1;
}
查看更多
趁早两清
3楼-- · 2019-02-21 05:22

As mentioned in the comments, the methods you use when the condition is true only apply to Google documents and will fail if you try to use them on other file types.

To prevent that simply add a second condition in your code like this :

if (filename == targetFile && file.getMimeType()=="application/vnd.google-apps.document") {

with the above modification you won't get errors anymore but there is a chance you won't be able to open the target you tried because it was not the right mime type.

查看更多
登录 后发表回答