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;
}
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:
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 :
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.