Google Apps Script that finds all Google Docs within a specific folder and adds a footer to each document with the provided text

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:

  1. Open Google Apps Script:
    • Navigate to Google Apps Script and sign in with your Google account.
    • Click on New Project.
  2. Rename the Project:
    • Click on Untitled Project at the top left corner and rename it to something meaningful, like AddFooterToDocs.
  3. Replace the Code:
    • In the script editor, replace the contents of the Code.gs file with the following script:
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();
  }
}
  1. 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.
  2. 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 the addFooterToAllDocsInFolder 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.
  3. 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.