Hey everyone, I’m trying to merge several Google Docs into one big document using Apps Script. All the docs are in the same folder and have a similar format. I’ve written some code to do this, but it’s not working as expected.
Here’s a simplified version of what I’m trying to do:
function joinDocs() {
const sourceFolder = DriveApp.getFolderById('FOLDER_ID');
const mergedDoc = DocumentApp.create('Combined Document');
const docs = sourceFolder.getFiles();
while (docs.hasNext()) {
const doc = docs.next();
const content = DocumentApp.openById(doc.getId()).getBody();
mergedDoc.getBody().appendParagraph(content.getText());
}
}
When I run this script, I get an error saying ‘Service unavailable: Documents’. I’ve already added the Docs API to my project, but it’s still not working.
Does anyone know what I’m doing wrong? How can I fix this to successfully merge all the docs into one? Any help would be awesome!
hey zack, try using the advanced drive service instead of documentapp. it’s more robust for large merges. add error handling and a short sleep to avoid limits.
function joinDocs() {
var docs = DriveApp.getFolderById('FOLDER_ID').getFiles();
var merged = DocumentApp.create('Combined');
while (docs.hasNext()) {
var doc = docs.next();
merged.getBody().appendParagraph(doc.getBlob().getDataAsString());
Utilities.sleep(500);
}
}
hope this helps!
I’ve dealt with this issue before, and it can be frustrating. One thing to consider is that the Documents service has limitations on how much content it can handle at once. Instead of appending entire documents, try breaking them down into smaller chunks. You could iterate through each paragraph of the source documents and append them individually to the merged doc. This approach is more memory-efficient and less likely to hit service limits.
Also, make sure you’re using the correct scope in your manifest file. You need ‘https://www.googleapis.com/auth/documents’ for full access to Docs API. If you’re still having trouble, you might want to look into using the advanced Drive service instead of DocumentApp, as it offers more robust methods for handling large amounts of document data.
I encountered a similar problem when merging documents with Apps Script. In my experience, the error ‘Service unavailable: Documents’ can occur if you exceed API usage limits or if the script times out while processing many documents. One solution that worked for me was to process documents in smaller chunks and insert a delay between processing each document. For example, I added a sleep function to give the service a small break, which helped reduce errors. You might also consider using the advanced Docs service for better handling of document contents.
Here is a revised code snippet:
function joinDocs() {
const sourceFolder = DriveApp.getFolderById('FOLDER_ID');
const mergedDoc = DocumentApp.create('Combined Document');
const docs = sourceFolder.getFiles();
while (docs.hasNext()) {
const doc = docs.next();
const content = doc.getBlob().getDataAsString();
mergedDoc.getBody().appendParagraph(content);
Utilities.sleep(1000); // short pause between documents
}
}
This method improved the reliability of my script and helped avoid timing out issues.