Exception: Service unavailable: Documents Error in Google Doc Apps Script - google-apps-script

I have an odd issue with a Google Doc Apps Script. It's been working for months without any issues. Then yesterday I get the Error Message: "Exception: Service unavailable: Documents" whenever I try to position an image blob (either from a URL Fetch or from getFileById). I simplified my code to this, and no matter what image I use, I always get the error.
function debugProc () {
var rLlogoborderlessId = '1ZhOX_aneaAhM0XJV4oQHGfNPUzaVETgu';
doc = DocumentApp.getActiveDocument();
curBody = doc.getBody();
var addlines = curBody.appendParagraph('');
var imgpara = curBody.appendParagraph("");
imgpara.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
var logoimage = DriveApp.getFileById(rLlogoborderlessId).getBlob();
var addlineslogo = curBody.appendParagraph('');
var k2nplogo = addlineslogo.addPositionedImage(logoimage);
// Error: Exception: Service unavailable: Documents
}
I tried multiple different computers and browsers. Nothing works. No workaround that I can find. How can I resolve this?

By reading your new comments I understand that the case isn't reproducible on new documents. The issue may have originated from a problem with your project or a bad configuration. To fix it, you only need to create a new Doc and copy all the data there. Feel free to leave a comment below if you need help with the migration.

Related

Apps Script (Error Exception: The document is inaccessible. Please try again later. Creator # Code.gs:10)

I wrote a script to automatically create and fill in the required fields in the file, but an error
Error Exception: The document is inaccessible. Please try again later. Creator # Код.gs:10
Please look at the code, tell me where I was wrong.
function Creator() {
const docFile = DriveApp.getFileById("1DhHBAd3ssYMgbbxJatK6_URxBPdoPk2K");
const tempFolder = DriveApp.getFolderById("16keE-_Dm0gglO42Lw0IphiPHYK-_TFXH");
const pdfFolder = DriveApp.getFolderById("1ZsmW9UPNaqgjt8kFh3y9h8tdBLAtj7gC");
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
var list = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var l=list.getLastRow();
for (var i=2; i <= l; i++) {
var a1 = list.getRange(i, 3).getValue();
var a2 = list.getRange(i, 4).getValue();
body.replaceText("{ПІБ}", a1);
body.replaceText("{Назва структури}", a2);
tempDocFile.saveAndClose();
const pdfFile = tempDocFile.getAs(MimeType.PDF);
pdfFolder.createFile(pdfFile).setName(a1);
tempFolder.removeFile(tempFile);
}
}
Assuming that the code line having the error is
var l=list.getLastRow();
It's very likely that you faced a Google environment glitch (I have faced the same error this weekend). In situations like this there is nothing else to do than to wait a bit and try again.
If you are developing a script / add-on / web-app to be used by others or you would like to handle this automatically you could use an algorithm like exponential backoff
If the error occurred on the previous code lines calling the Document Service, the problem might be caused for trying to open non Google Document file i.e. Microsoft Word file (.docx). In this case you should first convert the file to Google Document format and use the converted document instead of the original.
Related
Error message: "Cannot connect to Gmail" (Google services having glitches)
Google Script Document Inaccessible Error - creating and attempting to open document in code
Exception: The document is inaccessible. Please try again later. Google Scripts
Resources
Scripts implementing the exponential backoff algorithm in Google Apps Script
https://gist.github.com/peterherrmann/2700284
https://github.com/RomainVialard/ErrorHandler

DriveApp.getFileById() throwing a server error [duplicate]

This question already has answers here:
DriveApp Error: "We're sorry, a server error occurred. Please wait a bit and try again."
(4 answers)
Closed 1 year ago.
I have a script that I am using to export a google sheet to a PDF. It has been working no problem up until now. I deployed it as a Web App so that it could be used by anyone. Since I deployed it, it has not worked. I tried archiving the Web App and that did not help.
The issue seems to be with the DriveApp.getFileById() function. I inserted numerous console.log() functions along the way of my code to try to narrow down where it actually fails and why. The execution log shows that the error is happening at the same spot that I derived, but the console.log() functions did not help me figure out why. The error I receive is:
Exception: We're sorry, a server error occurred. Please wait a bit and try again.
I have tried researching this for quite some time now, and have tried suggestions that I found. I double checked all my variables and code syntax. I also know that the pdfid is being pulled correctly, as verified by logging it. I have not altered this part of the code, or any part that it depends on, at all since it has been working. I believe the Web App deployment is the issue, but I'm not sure why or how I could fix it. It could also be something else I'm missing, as it still does not work even after archiving the Web App.
Here is the code, the last line shown is where it fails:
function exportLog(type) {
console.log('Starting Export');
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var realExp = sheet.getSheetByName('exportThis');
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
//console.log('Spreadsheet created');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheet = realExp.copyTo(newSpreadsheet);
//console.log('Copied');
SpreadsheetApp.getActiveSpreadsheet().toast('Beginning export...','Loading...',-1);
//console.log('using new sheet');
newSpreadsheet.getSheetByName('Copy of exportThis').showSheet();
//console.log('successfully used new sheet');
newSpreadsheet.getSheetByName('Sheet1').activate();
newSpreadsheet.deleteActiveSheet();
//console.log('deleted');
var pdfid = newSpreadsheet.getId();
console.log(pdfid); // Correctly logs ID
var pdf = DriveApp.getFileById(pdfid);
function exportLog(type) {
const ss1 = SpreadsheetApp.getActive();//this returns the active spreadsheet
const xsh = ss1.getSheetByName('exportThis');
const ss2 = SpreadsheetApp.create("Spreadsheet to export");
const nsh = xsh.copyTo(ss2);
nsh.showSheet();//you can't really show the sheet because ss2 is not the active spreadsheet
ss2.getSheetByName('Sheet1').activate();//again you can't activate this sheet because ss2 is not the active spreadsheet
ss2.deleteActiveSheet();//This is not the active spreadsheet
const pdfid = ss2.getId();//I don't know why you call this a pdf it's a spreadsheet
const pdf = DriveApp.getFileById(pdfid); //again ss2 is a newly created spreadsheet
}
//Input parameter is never used
You should get in the habit of referring to the documentation while you write your code and pay special attention to the types that are returned by methods.

How to resolve "Too much calls for this service today : gmail" error in Google Apps script?

My question is about an error in Google's spreadsheet using gmail service in a function.
Since a while, an error occur when I run a function (On Google Spreadsheet) for retrieve mails on a label founded in the MailBox (Gmail).
The error message is : "Too much calls for this service today : gmail".
I want to specify that function worked fine before and it hasn't been modified.
The function is launched one time per month (Except in exceptional case)
I did some research on the error message, and the answers found confirmed what I thought,
daily quotas for Google's gmail services are exceed and can not be used until 24 hours.
However, it's the only one that has not worked, while others are working properly with these services without any errors.
Following this, I created a copy of the spreadsheet with the function to test if it isn't the sheet that does not work, but it has not changed.
And I launched it with another Google account, and it worked.
Does anyone know why this message appears please ?
Should we do a special manipulation to make it work again ?
Here is the row that sends an error :
var threads = GmailApp.getUserLabelByName("Label").getThreads();
And the function :
function readMail(){
var threads = GmailApp.getUserLabelByName("Label").getThreads();
var messages = GmailApp.getMessagesForThreads(threads);
for(var i in messages){
var message = messages[i];
for(var j in message){
var mess = message[j];
var sub = mess.getSubject();
if(mess.getTo().indexOf("email#gmail.com") > -1)
continue;
var attach = mess.getAttachments()[0];
var file = {
title: attach.getName()
};
var fileDoc = Drive.Files.insert(file, attach, {convert: false}); // Use Drive API
mess.markRead();
}
}
}

Why Google Apps Script throwing an "internal error" for Sites Services?

Here is my script, which basically iterate through drive folder, and put the file's blob as a attachment to some page on google site. It was working fine till day before yesterday, suddenly stop working after that.
function myFunction() {
var testpage = SitesApp.getSiteByUrl(siteURL).getChildByName("test");
var photofolder = DriveApp.getFolderById(folder_ID);
var filesinpf = photofolder.getFiles();
while(filesinpf.hasNext()){
var file = filesinpf.next();
var fblob = file.getBlob();
testpage.addHostedAttachment(fblob); //This line generating an error
}
}
Please help!
I had the same problem yesterday.
I have been working with this for a week, and yesterday I got an internal error in the last line:
function myFunction() {
var myFolder = DriveApp.getFolderById
("0B-ZOMOQnNEDOU9sWEV5SzlXVTQ");
var myFile =
myFolder.getFilesByName("Data.txt").next();
var myBlob = myFile.getBlob();
var myPage =
SitesApp.getSiteByUrl("https://sites.google.com/site/
demo ").getChildByName
("home/demoFileCabinet");
myPage.addHostedAttachment(myBlob);
}
Maybe a problem in Google Sites??. The code is correct.
I created an Issue Tracker too.
You can reproduce this error by using the sample code provided by google:
https://developers.google.com/apps-script/reference/sites/page#addHostedAttachment(BlobSource)
I created an enterprise support Ticket.
Keep you updated: [Case #14128120] Google Sites addHostedAttachment() not working
I got the following response from google:
Let me confirm that this is an issue on our end, filed with issue ID #68842220.
Please notice that our Engineering Team has already found the root cause for this.
At the moment, still, I can't confirm you when it will be fixed, but let me provide you with a simple workaround that will work while we wait for the fix: swap the "domain.com" and the "macros" parts of the URL when entering the URL in the dialog.
Sample, for url:
https://script.google.com/a/domain.com/macros/s/AKfycbwJfGpXIiWHfsCi-j66RuPMNx6kTFsdjYIbNOyufZptGA1tirm6/exec
try
https://script.google.com/a/macros/domain.com/s/AKfycbwJfGpXIiWHfsCi-j66RuPMNx6kTFsdjYIbNOyufZptGA1tirm6/exec

modal dialog with new spreadsheet api issue

I'm was trying to show html body of gmail message in modal dialog window but faced an error calling SpreadsheetApp.getUi() method.
...
var html = HtmlService.createHtmlOutput(threads[i].getMessages()[0].getBody());
SpreadsheetApp.getUi().showModalDialog(html, 'My add-on');
...
How I can call that dialog? Or may be there is better aproach to display message(some panel or sidebar)?
This prompt box is misleading, as the error must be elsewhere. The new version of Sheets not only does support the getUI method, I believe it's specifically designed for for the new version of sheets.
I've expanded on your previous question and tested this myself with the following code in the new version of sheets:
function getMail() {
var threads = GmailApp.getInboxThreads();
var messages = threads[0].getMessages()[0];
var raw = messages.getPlainBody();
return raw;
}
function dialogueBox(){
var raw = getMail();
var htmlOutput = HtmlService.createHtmlOutput(raw);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'For mk_yo');
}
and it displays the prompt with no issues, as shown here. Try creating a new sheet with a new script and run the code above. Additionally, you can try ensuring that the sheet that you're adding this to is definitely using the new version of sheets.
In relation to if there's a better approach by displaying a side panel, yes you can use custom sidebars in the new version of Google sheets(and only the new version), but as this still uses the 'getUI' method, this won't resolve your current error, as this is not where the issue lies.