This script is for creating name badges from a list of names on a spreadsheet. Its takes rows of data from a Google spreadsheet and writes them to a Google Document in a formatted way. When I ran it a week ago, it worked fine with no errors.
Today I started getting the following error every time I ran the script:
Service Documents failed while accessing document with id 19Ujy457kSUyQs03O2Xp6zBmulzcxHkb17Bi7w0xMCDI.
The id in the error refers to the the document where I'm trying to write the values.
Here is my Google Apps Script code:
function createBadges() {
//Ids for docs and sheets
var docTemplateId = "1VYuyFS_t5ha-7fDLhhWHOFxgeELr7QOF23sMLTnzy9E";
var docFinalId = "19Ujy457kSUyQs03O2Xp6zBmulzcxHkb17Bi7w0xMCDI";
// var docFinalId = DocumentApp.create('23-24 FamU Makeup Badges').getId();
var ssID = "1X9aAe5M5uQyYQ0JzYL2ZWi31NPQUdG93TDw_XbpHNWI";
//Get the docs and sheet
var docTemplate = DocumentApp.openById(docTemplateId);
var docFinal = DocumentApp.openById(docFinalId);
var sheet = SpreadsheetApp.openById(ssID).getActiveSheet();
Logger.log(sheet)
var data = sheet.getRange(2, 1, sheet.getLastRow()-1,10).getValues();
var templateParagraphs = docTemplate.getBody().getParagraphs();
docFinal.getBody().clear()
var batchSize = 6;
for (var i = 0; i < data.length; i += batchSize) {
var rowsToProcess = data.slice(i, i + batchSize);
createPageWithBadges(rowsToProcess, templateParagraphs, docFinal);
}
const body = docFinal.getBody();
body.setMarginLeft(42)
body.setMarginRight(30)
body.setMarginTop(30)
body.setMarginBottom(6)
}
function createPageWithBadges(rows, templateParagraphs, docFinal) {
if (docFinal.getBody().getText() !== "") {
docFinal.getBody().appendPageBreak();
}
// Create a table with two rows and two columns
var table = docFinal.getBody().appendTable([
["", ""], // First row with two cells
["", ""], // Second row with two cells
["", ""]
]);
table.setColumnWidth(0, 260); // Adjust the column width as needed
table.setColumnWidth(1, 240); // Adjust the column width as needed
table.setBorderWidth(0)
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var cell = table.getCell(Math.floor(i / 2), i % 2);
var copiedParagraphs = templateParagraphs.map(function (p) {
return p.copy()
.replaceText("{lastName}", row[0])
.replaceText("{firstName}", row[1])
.replaceText("{famNumber}", row[2])
.replaceText("{gradeLevel}", row[3]==='Parent'?`Parent` :`Grade ${row[3]}`)
.replaceText("{first}", row[6])
.replaceText("{class1}",row[8])
.replaceText("{class2}",row[9])
.replaceText("{second}", row[7]);
});
copiedParagraphs.forEach(function (copiedParagraph) {
cell.appendParagraph(copiedParagraph);
});
}
}
This code combines 6 rows of data from the Spreadsheet onto one page of the Google Document.
I know there are similar issues already on Stack Overflow, but none of the solutions appear to apply to this example. As far as I know, this document has no footers, the google sheet tab is retrieved correctly, the spreadsheet is already saved as a Google Sheet (not an excel file),and I have adequate permissions to run this script.
What could be causing this script to malfunction when it worked fine just a week ago? Could it be an issue of runtime quotas? Thank you for your help.
Considering that running the script again produced no errors, it's likely that none of Apps Script's documented quotas are the culprit. However, Apps Script does have limits that aren't published and that may change without notice. If you can reliably get the same error after running the script a certain number of times, there's a high likelihood that you're hittin at least one of those undocumented limits.
Lastly, there's an existing bug regarding the error that you're getting. In case you encounter the same error again, do click the +1
icon on the upper right of the issue tracker page to let Google know that you're affected as well.