o create a Google Apps Script that finds all Google Docs within a specific folder and adds a footer to each document with the provided text, follow these steps:
- Open Google Apps Script:
- Navigate to Google Apps Script and sign in with your Google account.
- Click on
New Project
.
- Rename the Project:
- Click on
Untitled Project
at the top left corner and rename it to something meaningful, likeAddFooterToDocs
.
- Click on
- Replace the Code:
- In the script editor, replace the contents of the
Code.gs
file with the following script:
- In the script editor, replace the contents of the
function addFooterToAllDocsInFolder() {
const folderId = 'YOUR_FOLDER_ID_HERE'; // Replace YOUR_FOLDER_ID_HERE with your actual folder ID
const footerText = 'Learn more about coding with Examples and Source Code Laurence Svekis Courses https://basescripts.com/';
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFilesByType(MimeType.GOOGLE_DOCS);
while (files.hasNext()) {
const file = files.next();
const doc = DocumentApp.openById(file.getId());
const footerSection = doc.getFooter();
// If the document does not have a footer, create one
if (!footerSection) {
doc.addFooter().appendParagraph(footerText);
} else {
// If a footer exists, append the text to the existing footer
footerSection.appendParagraph(footerText);
}
// Save and close the document
doc.saveAndClose();
}
}
- Add Your Folder ID:
- Replace
YOUR_FOLDER_ID_HERE
in the script with the ID of the folder that contains your Google Docs. You can find the folder ID in the URL when you open the folder in Google Drive.
- Replace
- Save and Run the Script:
- Click the disk icon or
File > Save
to save your script. - To run the script, click the play button (
▶
) next to theaddFooterToAllDocsInFolder
function in the dropdown menu. - The first time you run the script, you will need to authorize the script to access your Google Drive and Docs. Follow the prompts, review the permissions, and allow the necessary access.
- Click the disk icon or
- Check Your Documents:
- After running the script, check the documents in the specified folder to see if the footer has been added correctly.
This script will iterate through all Google Docs in the specified folder, adding the designated footer text to each document. If a document already has a footer, it will append the new footer text to the existing footer. If it doesn’t have one, it will create a new footer with the provided text.