4 Approaches to extracting the body text of a Google Doc with Google Apps Script. Full Code and tutorial
Scott Donald must be one of the most comprehensive Google Apps Script writers I know. All of his tutorials are packed with information and useful tips. This recent post is no exception, as Scott dives in and shares this detailed guide on retrieving a Google Docs body text using Google Apps Script.
The tutorial explores four approaches to extracting text from a Google Doc:
DocumentApp: This approach is straightforward for basic text extraction but may not capture all elements, especially “Smart Chips.”
DocumentApp with Element Iteration: This method allows for extracting text and URLs from standard text and supports some “Smart Chips” like Date, Rich Link, and Person.
OCR Approach: This involves converting the document to a PDF, applying OCR, and reading the extracted text. It captures most displayed text but may not recognise emojis or some formula symbols.
Docs API Advanced Service: This approach utilises the Docs REST API to access text, links, and specific “Smart Chip” data. It offers more control over data extraction but may require navigating complex JSON responses.
Scott’s tutorial provides a comprehensive and practical guide to retrieving Google Docs body text using Google Apps Script. Be sure to check out the full tutorial for detailed explanations, code examples, and helpful tips. And don’t forget to share your preferred approach and any challenges you’ve encountered on Scott’s post.
Starting today, the OAuth consent screen will now let users specify which individual OAuth scopes they would like to authorize. For example, if a script requests access to a user’s Sheets and Forms files, and the users only intends to use the script with Sheets files, they can decide to only allow access to their spreadsheets and not their forms. This affords users the benefit of more granular control over what data their 3P applications are allowed to access.
Exciting news for Apps Script developers and users! Google has taken the first step towards more granular control over OAuth consent in Apps Script. This means users can now choose exactly which services their scripts can access, leading to a more secure and transparent experience.
What’s New?
Previously, when running a script that required access to multiple services (like Google Sheets and Forms), users had to grant permission to all or nothing. This all-or-nothing approach could make some users hesitant to use scripts, especially if they were only comfortable granting access to a subset of the requested services.
With the latest update, the OAuth consent screen now allows users to authorize individual services. For instance, if a script needs access to both Google Sheets and Forms, but the user only intends to use it with Sheets, they can simply grant access to Sheets and deny access to Forms.
This change is currently being gradually rolled out to the Apps Script IDE, with expected completion by January 24, 2025.
Enhanced Security and Transparency
This granular control over OAuth consent is a significant step towards improving the security and transparency of Apps Script. Users now have more control over their data and can make informed decisions about which services they want to allow their scripts to access.
This change also benefits developers, as it can help build trust with users and encourage wider adoption of their scripts.
A Glimpse into the Future: Chat Apps and Workspace Add-ons
While currently limited to the Apps Script IDE, this granular OAuth consent model will eventually be implemented in other Apps Script surfaces, notably Chat apps and Workspace Add-ons. This has implications for developers working in these areas.
For Chat app and Workspace Add-on developers, this means it’s time to start thinking about how this granular consent model will impact your development process and user experience. You can start experimenting with the new methods added to the ScriptApp and AuthorizationInfo classes to programmatically handle different consent scenarios.
Digging Deeper: AuthorizationInfo
To help developers manage these granular permissions, Google has introduced the AuthorizationInfo class. This class provides methods to check authorization status, retrieve authorized scopes, and even generate authorization URLs. Here’s a quick look at some of its key functionalities:
getAuthorizationStatus() : This method allows you to determine if a user needs to authorize the script to use one or more services.
getAuthorizationUrl() : This method returns an authorization URL that can be used to grant access to the script.
getAuthorizedScopes() : This method returns a list of authorized scopes for the script.
These methods give developers fine-grained control over the authorization process, enabling them to build more robust and user-friendly applications. Google have already published a great developer guide on how to Handle granular OAuth permissions.
Learn More
To learn more about this update, be sure to check out the official announcement provided in the source link.
Discover a treasure trove of scripts at The Awesome Script Site. Developed primarily for Google Workspace, these scripts offer solutions to common challenges faced by businesses and organizations. Created by Jonas Lund and soon other contributors, these scripts are free to use and come with comprehensive documentation. Share this resource with your network and remember to visit often for new additions.
The Awesome Script Site, by Jonas Lund, is a “treasure trove of scripts” for both users and Google Workspace administrators. The administrator scripts simplify various administrative tasks, but you will require a Google Workspace admin role to use them. Due to their handling of sensitive user data obviously please take time to review the scripts to make sure you are happy with how they work.
The scripts cover a wide range of functions, including:
Alias Management: Manage user email aliases directly from a Google Sheet.
Chromebook Reporting: Fetch and organize Chromebook data from your Google Workspace.
Group Member Management and Reporting: Bulk add or remove group members and generate group member reports.
OU Management: Create, list, and delete Organizational Units (OUs) based on specific criteria.
It’s worth spending a little time exploring the Awesome Scripts site to discover these admin scripts and more. To keep up-to-date on new ‘Awesome Scripts’ you can follow Jonas Lund on LinkedIn. Follow the source link to get the scripts and read more.
When working with Google Sheets, you often use formulas like IMPORTRANGE to transfer data between files. However, IMPORTRANGE is not always reliable, especially when files become too large and you see the message “The file is too large.” In such cases, switching to a Google Apps Script-based solution that uses CSV files for data export and import is more efficient and flexible.
Here’s how you can create a system to export data from a Google Sheet to a CSV file and then import it into another sheet.
Export Data Script
This script exports data from a sheet named YOUR_TAB_NAME to a CSV file saved in Google Drive.
function exportToCSV() {
const SheetName = "YOUR_TAB_NAME";
const NameFile = "YOUR_FILE_NAME";
const ss = SpreadsheetApp.getActive();
const sourceSheet = ss.getSheetByName(SheetName);
// Get the values as displayed in the sheet
const dataValues = sourceSheet.getDataRange().getDisplayValues();
// Creating CSV content
const csvContent = dataValues.map(row => row.join(";")).join("\n");
// Check if there is data
if(csvContent == "") return;
try {
const fileId = DriveApp.getFilesByName(NameFile + ".csv").next().getId();
DriveApp.getFileById(fileId).setContent(csvContent);
} catch {
DriveApp.createFile(NameFile + ".csv", csvContent, MimeType.CSV);
}
}
How It Works
Fetches data from the specified sheet (YOUR_TAB_NAME).
Creates CSV content, joining data with the ; separator.
Updates the CSV file if it already exists or creates a new one.
Import Data Script
This script imports data from a CSV file into a Google Sheet named YOUR_TAB_NAME.
function importFromCSV() {
const SheetName = "YOUR_TAB_NAME";
const NameFile = "YOUR_FILE_NAME";
const Separator = ";"; // Change your separator if needed
const destinationSheet = SpreadsheetApp.getActive().getSheetByName(SheetName);
let fileId;
try {
// Search for the file and get its ID
fileId = DriveApp.getFilesByName(NameFile + ".csv").next().getId();
} catch {
Logger.log("Il file '" + NameFile + ".csv' non è stato trovato in Google Drive.");
return;
}
const file = DriveApp.getFileById(fileId);
const property = PropertiesService.getDocumentProperties();
// Check if the last imported data has already been loaded
const lastModified = property.getProperty("lastModified");
const timeStamp = file.getLastUpdated().toUTCString();
property.setProperty("lastModified", timeStamp);
if (lastModified == timeStamp) return;
// Retrieve the CSV content
const csvContent = file.getBlob().getDataAsString();
// Split the content into rows and then into columns using the specified separator
const data = csvContent
.split("\n") // Split into rows
.map(row => row.split(Separator)); // Split each row into columns using the specified separator
destinationSheet.clearContents();
destinationSheet.getRange(1, 1, data.length, data[0].length).setValues(data);
}
How It Works
Finds the CSV file in Google Drive.
Checks for changes by comparing the file’s last modification time with a stored property value.
Reads data from the CSV file and imports it into the sheet, clearing existing data first.
Automation with Triggers
To automate the import process, you can use a time-based trigger to run the script at regular intervals (e.g., every minute, hour, etc.).
Setting Up a Trigger
Go to Apps Script Editor (Extensions > Apps Script).
Click on Triggers (Clock icon) or Tools > Triggers in the editor.
Create a new trigger:
Choose the importFromCSV function.
Select “Time-driven trigger.”
Specify the frequency (e.g., every minute).
With this trigger, your script will regularly check for updates in the CSV file and automatically import new data.
Explanation of the Separator Usage
Why the semicolon (;) is used: Descriptions or text fields may already use commas (,), and using them as a separator could lead to incorrect data splits. Using semicolons avoids this issue.
Alternative separator: If semicolons (;) are also present in the data, it’s recommended to use a unique symbol, such as §.
To update the separator, replace “;” with “§” in the following line:
.map(row => row.split("§"));// Update the separator here
This makes the script adaptable to various data scenarios.
Why Use This Method?
Advantages
Avoid IMPORTRANGE Limits: No errors related to overly large files.
Efficiency: Data is transferred as CSV, reducing connectivity issues between files.
Automation: Imports happen without manual intervention.
Limitations
Maintenance: Scripts need to be managed and updated manually if changes occur.
Security: Ensure file and script access is secure.
With these scripts and a configured trigger, you can reliably and efficiently handle large volumes of data between Google Sheets. If you need further customizations or help setting it up, feel free to ask!
Bluesky is gaining traction this end of 2024, and if you’re on the platform, you need to know your numbers. Want to track your Bluesky stats without the hassle? Google Apps Script and Google Sheets are here to rescue you with a simple, yet powerful solution.
In this blog post, Stéphane Giron provides a guide to tracking Bluesky social media statistics using Google Apps Script and Google Sheets. With the growing popularity of Bluesky it can be useful and interesting to understand the social media dynamic of the platform.
The script he offers tracks metrics such as the number of posts, followers, likes, reposts, and more. Stéphane also details how to install and set up the script, including copying and pasting the provided code, running initialization functions, and scheduling automatic data collection. The result is raw data that users can then use to create graphs and visualize their Bluesky activity over time.
Google Apps Script automates tasks like managing protections in Google Spreadsheets. These protections control user access to specific cells. While scripts exist for this purpose, users still encounter challenges, prompting this report. The report aims to introduce techniques for managing protections using sample scripts, helping users understand and implement this functionality.
Google Sheets aficionados are likely no strangers to the “Protect sheet” and “Protect range” options tucked away in the menus. These features offer a basic level of control over who can edit what within your spreadsheet. But what if you need more dynamic, automated control over these protections? That’s where the power of Google Apps Script and the Sheets API comes into play.
This post from Kanshi Tanaike provides a deep dive into how you can programmatically manage protections in your Google Sheets. While the traditional menu options are great for static scenarios, using Google Apps Script allows you to create more flexible and powerful protection workflows.
Why Go Script?
Dynamic Protections: Instead of manually adjusting protections, you can use scripts to change them based on specific conditions or events within your spreadsheet.
Automation: Integrate protection changes into larger automation workflows, streamlining processes and reducing manual intervention.
Granular Control: Achieve a level of control over cell-level permissions that goes beyond the standard menu options.
Some possible use cases for developers could include:
Approvals Automation: Imagine a scenario where certain parts of a spreadsheet need to be locked down once a manager approves them. With this solution, you could create a script that automatically protects those ranges upon approval.
Time-Limited Editing: Need to open up a section of a spreadsheet for editing only during a specific window of time? You could use Google Apps Script to handle this, automatically protecting
The scripts provided by Kanshi Tanaike offer a starting point for exploring these possibilities.
Quickly copy or move existing files into folders within Google drive via a Google Sheet.
Quickly copy or move existing files into folders via a Google Sheet
The following Google Apps Script tool allows you to quickly copy or move existing files into existing folders within Google/Shared drive. This is quite a niche tool so it is anticipated you would already have a Google Sheet of file IDs/URLs, though there is the option to select an existing Drive folder of files and automatically bring them into this tool.
You can continue to append further rows to the Google Sheet as existing ones will be skipped once the ‘Status’ column has automatically been marked as ‘Completed’. Alternatively you can clear the Sheet each time for a fresh start.
I use Google Apps Script to support staff and students in my job. I enjoy dabbling with creating tools to help with automation and I freely share my learning experiences on my blog, where I also have a number of useful Google Add-ons: www.pbainbridge.co.uk
Automate publication of your script from development to production easier.
This post by Stéphane Giron provides a CI/CD solution for Google Apps Script, which makes it easy for you to automate the deployment of Apps Script code from a development version to production. The solution uses the Apps Script API to manage versions and streamline the code copying process.
Stéphane has previously created a backup solution for Google Apps Script code, but it lacked version control and multi-file support. This new CI/CD solution addresses these limitations by using the Apps Script API to handle version management and the publication process.
The post includes the necessary code and configuration for implementing this solution. To use this solution you will need to enable the Apps Script API and as well as setting up your project with a standard Google Cloud Platform (GCP) Project.
As a bonus the solution also allows you the option of restoring previous versions of your deployed script, providing a rollback mechanism in case of errors or issues. This solution is lighter weight than other CI/CD setups featured in Pulse, but is easier to set up, providing a practical approach to implementing CI/CD for Google Apps Script projects and enhancing code management and deployment processes.
Recently, I worked on a project with a client who needed to parse XML files and convert them to JSON. Simple enough, right? The catch? These XML files were massive — each containing hundreds or even thousands of trades. Naturally, the first thing I thought of was Google Apps Script’s built-in XMLService. It’s reliable, widely used, and integrates smoothly into Apps Script. But when I ran it on a file with 1,000 trades, the parsing took 45 seconds! That’s painfully slow for an automation process.
[Editor: In a recent blog post, Dmitry Kostyuk has shared a valuable tip for developers working with Google Apps Script. When faced with the task of parsing large XML files, he discovered that the built-in XMLService was causing significant slowdowns. For files containing thousands of records, the parsing process could take an agonizing 45 seconds.
Dmitry’s solution? Using his Apps Script Engine (ASE) and the xml-js NPM library. By making this switch, he was able to achieve a remarkable 10x increase in parsing speed, reducing the 45-second process to only 4 seconds.
If you’re working with Google Apps Script and large XML files, click the source post link to find out how you can give ASE and xml-js a try!]
Google Apps Script offers Document service for basic document tasks and Google Docs API for advanced control, requiring more technical expertise. This report bridges the gap with sample scripts to unlock the API’s potential.
Kanshi Tanaike’s latest blog post, “Unlocking Power: Leverage the Google Docs API Beyond Apps Script’s Document Service,” is a great read for Google Workspace developers. It offers a goldmine of code snippets that will supercharge your Google Docs projects. Tanaike highlights the advantages of using the Google Docs API for more advanced features and flexibility compared to the standard Apps Script DocumentApp Service. Even though the API requires a bit more technical know-how, Tanaike provides clear, practical examples to get you started.
Some of the sample snippets include:
Changing page orientation (portrait to landscape and vice versa)
Kanshi Tanaike’s post serves as a valuable resource for developers looking to expand their Google Docs capabilities. Hopefully with these sample scripts, you can start unlocking new levels of automation and customization in your Google Doc projects.
To delve deeper into the world of Google Docs API, follow the source link post.