Created
December 21, 2024 22:38
-
-
Save TomFaulkner/5aba2a5c1f715af2718328c37828b9e5 to your computer and use it in GitHub Desktop.
Google Docs - Replace a Watermark on a directory of Docs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Set these three variables. | |
/// They should exist so the paths are: | |
/// sourceDirName | |
/// watermark-template (can be renamed, change the variable) | |
/// lots of Docs files | |
/// | destDirName | |
/// | new files will be here | |
/// That is, sourceDirName is a directory that contains watermark-template, destDirName, and some number of documents. | |
/// Please copy files into the sourceDirName directory rather than trusting this to process all at once or to not break things. | |
/// Verify new files. | |
/// In the source dir select: New >> More >> Google Apps Script | |
/// Paste into the editor. | |
/// Save Project | |
/// Click Run | |
/// Source: Grok saved a lot of time writing this, it might not be ideal, but it works. | |
var sourceDirName = "original"; | |
var destDirName = "watermarked"; | |
var waterMarkTemplateName = 'watermark-template' | |
function getFileIdByName(name) { | |
var files = DriveApp.getFilesByName(name); | |
if (files.hasNext()) { | |
var file = files.next(); | |
Logger.log(file.getId()); // Logs the file ID | |
return file.getId(); | |
} | |
} | |
function copyContentToWatermarkedDocForFolder() { | |
// ID of the folder containing the source documents | |
var sourceFolderId = DriveApp.getFoldersByName(sourceDirName).next().getId(); | |
console.log(`Using ${sourceDirName}: ${sourceFolderId}`) | |
var destFolderId = DriveApp.getFoldersByName(destDirName).next().getId(); | |
console.log(`Output ${destDirName}: ${destFolderId}`) | |
// ID of the template document with the watermark (destination) | |
var watermarkedTemplateId = getFileIdByName(waterMarkTemplateName); | |
if (watermarkedTemplateId === undefined) { | |
throw new Error(`Template file was not found, or not named ${waterMarkTemplateName}`) | |
} | |
console.log(`Using ${watermarkedTemplateId}`) | |
// Get the folder | |
var sourceFolder = DriveApp.getFolderById(sourceFolderId); | |
var destinationFolder = DriveApp.getFolderById(destFolderId); // Assuming you want to save in the same folder | |
// Open the watermarked template document | |
var templateFile = DriveApp.getFileById(watermarkedTemplateId); | |
var templateDoc = DocumentApp.openById(watermarkedTemplateId); | |
// Iterate through all files in the source folder | |
var files = sourceFolder.getFilesByType(MimeType.GOOGLE_DOCS); | |
while (files.hasNext()) { | |
var file = files.next(); | |
var fileName = file.getName(); | |
if (fileName == waterMarkTemplateName) { | |
continue; | |
} | |
// Check if a watermarked copy already exists | |
var existingWatermarkedFile = destinationFolder.getFilesByName(fileName); | |
if (existingWatermarkedFile.hasNext()) { | |
Logger.log('A watermarked copy of ' + fileName + ' already exists. Skipping.'); | |
continue; | |
} | |
try { | |
var sourceDoc = DocumentApp.openById(file.getId()); | |
var sourceBody = sourceDoc.getBody(); | |
// Create a new document from the template with watermark using the File object | |
var newFile = templateFile.makeCopy(fileName + ' - Watermarked', destinationFolder); | |
var newDoc = DocumentApp.openById(newFile.getId()); | |
var newDocBody = newDoc.getBody(); | |
// Clear any existing content in the new document except for the watermark | |
newDocBody.clear(); | |
// Copy all content from the source document to the new document | |
var numChildren = sourceBody.getNumChildren(); | |
for (var i = 0; i < numChildren; ++i) { | |
var element = sourceBody.getChild(i).copy(); | |
var type = element.getType(); | |
switch(type) { | |
case DocumentApp.ElementType.PARAGRAPH: | |
newDocBody.appendParagraph(element); | |
break; | |
case DocumentApp.ElementType.TABLE: | |
newDocBody.appendTable(element); | |
break; | |
case DocumentApp.ElementType.LIST_ITEM: | |
newDocBody.appendListItem(element); | |
break; | |
case DocumentApp.ElementType.IMAGE: | |
newDocBody.appendImage(element); | |
break; | |
case DocumentApp.ElementType.INLINE_IMAGE: | |
newDocBody.appendInlineImage(element); | |
break; | |
case DocumentApp.ElementType.HORIZONTAL_RULE: | |
newDocBody.appendHorizontalRule(element); | |
break; | |
case DocumentApp.ElementType.PAGE_BREAK: | |
newDocBody.appendPageBreak(element); | |
break; | |
case DocumentApp.ElementType.HEADING: | |
newDocBody.appendParagraph(element); // Treats heading as a paragraph | |
break; | |
case DocumentApp.ElementType.FOOTNOTE: | |
newDocBody.appendFootnote(element); | |
break; | |
// Add other element types as needed | |
default: | |
Logger.log('Unhandled element type: ' + type); | |
} | |
} | |
// Optionally, save the new document | |
newDoc.saveAndClose(); | |
// Log the URL of the new document for reference | |
Logger.log('Created new document with watermark: ' + newDoc.getUrl()); | |
} catch (error) { | |
// Log errors for each document if any arise | |
Logger.log('Error processing document ' + fileName + ': ' + error); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At this time, Google's API doesn't provide a way to replace watermarks programmatically. This copies the contents to a copy of a template file.