1

I have a script that grabs a template, copies it, then replaces certain fields with data in a spreadsheet. It was working beautifully yesterday, but today I get this error:

"Service unavailable: Docs" (with reference to the line I've * below

I haven't touched the code, but suddenly this error. Any ideas why?

var file = DocsList.find("my TEMPLATE")[0];
var copy = file.makeCopy("my DOCUMENT");
var copyId = copy.getId(); //***This is the line the error points to
var docCopy = DocumentApp.openById(copyId);
var body = docCopy.getBody();
body.replaceText('{date}', Utilities.formatDate(new  Date(sheet.getRange('A1').getValues()), "GMT", "MMM dd"));

By the way, when I searched this error online all the discussions related to tables, but I'm not copying or creating any tables in this script.

Thanks for your help!

duck
  • 149
  • 1
  • 4
  • 15
  • Is it working now? How about... now? I've seen that sometimes pop up as a transient. – Mogsdad Dec 06 '13 at 02:56
  • I've run it probably 100 times in the past hour, hoping it would just go away, but no luck! – duck Dec 06 '13 at 02:58
  • Something's broken. I added `var name = file.getName();` right after the first line of your code, and found that the result was the SCRIPT file, not the named template. On `makeCopy()` I got a weird error message saying `File WTF must be converted to a Google document first.` I've never seen that before. – Mogsdad Dec 06 '13 at 03:18
  • Okay, so I just noticed that in my Drive folder it shows a copy of the script file with the file name that was intended for the copy of the document template. Yikes. – duck Dec 06 '13 at 03:26
  • Okay, now it's working fine this morning. Serge, you there? Any ideas why this is happening? The script will run weekly, so I've got to trust that it's going to work each time. Thanks for your time. – duck Dec 06 '13 at 14:14
  • Ha! spoke too soon. Just ran the script again and the error is back :( – duck Dec 06 '13 at 14:17

2 Answers2

1

There's definitely a bug with DocsList.find() at this time. Issue 3470 has been raised, please star it and hope it gets some attention.

Consider this snippet, from a stand-alone script named "WTF":

function myFunction() {

  var files = DocsList.find("Testdoc");
  for (var f in files) {
    // All files found MUST be named the same, right?
    Logger.log(files[f].getName());
  }
}

Here's what the logs contain:

[13-12-06 09:46:07:836 EST] WTF
[13-12-06 09:46:07:836 EST] Testdoc        <<<<<<< That's the one!
[13-12-06 09:46:07:837 EST] Doc from Doc

Here's the same search, run later in the day. Note that the source script is no longer in the list... with this result, item [0] would contain the file we were looking for:

[13-12-06 17:11:08:481 EST] Testdoc
[13-12-06 17:11:08:482 EST] Doc from Doc

So we can't always trust DocsList.find(). As @Sergeinsas points out,find()should be returning all files thatcontain` the query text - so trusting that our file name is the only match would be unreliable.

To be sure to get a match on the file name, here's a work-around helper function:

function docFind(filename) {
  var files = DocsList.find(filename);
  for (var f in files) {
    if (filename === files[f].getName()) return files[f];
  }
  // Not found
  return null;
}

With that, just change one line and your code becomes:

var file = docFind("my TEMPLATE");
var copy = file.makeCopy("my DOCUMENT");
var copyId = copy.getId(); //***This is the line the error points to
var docCopy = DocumentApp.openById(copyId);
var body = docCopy.getBody();
body.replaceText('{date}', Utilities.formatDate(new  Date(sheet.getRange('A1').getValues()), "GMT", "MMM dd"));
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • This time (and for once) I don't agree with you ... if you refer to the [documentation](https://developers.google.com/apps-script/reference/docs-list/docs-list?hl=fr-FR#find(String)), DocsList.find('query') *Returns an array of all the files in the container that contain the given string.* This doesn't mean files **named** query but containing 'query' so I guess that's not a bug but the expected behavior. At least that's how it used to work for a long time and how I've always used it. – Serge insas Dec 06 '13 at 15:25
  • See this post from Corey G that uses this feature :http://stackoverflow.com/questions/14770700/how-do-i-locate-the-path-of-the-folder-in-which-the-current-script-file-is-locat/14778677#14778677 – Serge insas Dec 06 '13 at 15:42
  • Side question: why does the newly created file show a "Last Modified" time that is 3 hours before my time zone? – duck Dec 06 '13 at 15:44
  • Google is in possession of a Tardis. (Or check all your locale settings - account, file, etc.) – Mogsdad Dec 06 '13 at 16:25
  • @Sergeinsas - Agreed about the docs - bad of me to focus on just the file name. EDITED. However, `find()` doesn't always find the running script. (I just re-ran that code above, and it has reverted to not finding the source script anymore. Corey's example doesn't work for me at this moment, either.) Further, that third doc in my search results does not contain the string "Testdoc" (it does contain "TEST DOC BLOB") implying a fuzzy search... but it missed other file containing "This is a test document." There's something more to this... – Mogsdad Dec 06 '13 at 22:37
  • I didn't take the time to check right now... I'll update tomorrow with test results. I have a few scripts using it so it should be easy to check. Thanks for update. – Serge insas Dec 06 '13 at 23:00
0

Mogsdad, you're a real warrior. Thanks for all the time and effort you put into these communities. I've only been teaching myself scripting for 4 days now, and I've lived by the answers you and Serge regularly post, including to a few of my own questions. You guys rock.

I'll give your workaround a try. Also, I found some code on this page:

http://tjhouston.com/2012/03/merge-info-from-google-forms-to-pdf-document-and-send-via-email/1

Using this example, I modified my script to the following. I've run it a few times and it works correctly. However, I'll have to run it several times over several days to be sure that the error doesn't creep back up.

var docTemplate = "[insert template doc key here]"; //this one uses the document key, not the file name like mine did
var copy = DocsList.getFileById(docTemplate).makeCopy("my DOCUMENT");
var copyId = copy.getId();
var copyDoc = DocumentApp.openById(copyId);
var body = copyDoc.getActiveSection();
body.replaceText('{date}', Utilities.formatDate(new Date(sheet.getRange('A1').getValues()), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "MMM dd"));
duck
  • 149
  • 1
  • 4
  • 15