AppsScriptPulse

Using Google Drive API and Google Apps Script to convert between Google Docs and Markdown

Great news for fans of both Google Docs and Markdown! Google Docs recently acquired the ability to export documents directly into the markdown format.

This functionality extends beyond the user interface, with early indications suggesting the Google Drive API might also be capable of converting between Google Docs and Markdown. I confirmed that this could also be achieved by Drive API. This opens exciting possibilities for automated workflows.

Google recently announced in July 2024 that import and export Markdown in Google Docs. This is a user facing features, which Google announced includes the ability to:

  • Convert Markdown to Docs content on paste
  • Copy Docs content as Markdown
  • Export a Doc as Markdown (from File > Download)
  • Import Markdown as a Doc (from File > Open or “Open with Google Docs” from Drive)

Kanshi Tanaike hasn’t wasted any time in unpicking Markdown conversion capabilities using the Google Drive API. This functionality enables automated workflows for converting between Google Docs and Markdown. There are various scenarios where this can be useful, in particular, given GenAI solutions like the Gemini API often generate markdown there are opportunities to automatically convert these to Google Docs.

As part of the source post there are sample scripts: one for converting Google Docs to Markdown and another for converting Markdown to Google Docs.

For the Markdown to Google Docs it is assumed that there is already a Markdown file in Google Drive. If you have a Markdown text as a string, for example from a Gemini API response, then you can create a formatted Google Doc using the following snippet designed to be used with v3 of the Google Drive Advanced Service:

function sample3() {
  // note string is tab sensitive (tabs are converted to code blocks on certain Workspace accounts)
  const sampleText = `sample text 1

| a1 | b1 | c1 |
| :---- | :---- | :---- |
| a2 | b2 | c2 |

sample text 2

* sample option1
* sample option2
* sample option3

sample text 3`;

  const blob = Utilities.newBlob(sampleText, 'text/markdown');
  const fileMetadata = {
    name: `Sample MD Conversion`,
    mimeType: MimeType.GOOGLE_DOCS,
  };

  Drive.Files.create(fileMetadata, blob, { supportsAllDrives: true });
}

Source: Convert Google Document to Markdown and vice versa using Google Apps Script

Automate Google Drive test data creation with Google Apps Script

This post describes a Google Apps Script function to create a dummy folder structure in Google Drive, including nested folders and files (Documents, Sheets, Slides). It highlights some useful features beginners might find useful such as recursion, randomness, and the use of Google Apps Script services for Drive interactions. The post additionally discusses specific challenges encountered, like using Drive.Files.create for file creation and ensuring compatibility with Shared Drives.

Working with Google Workspace Enterprise customers we recommend using a DEV/Test Google Workspace domain. If you purchase your Workspace licences through a Google Partner you should be able to get a free test domain, which admins can use to test features like Data Loss Prevention, Context Aware-Access without fear of breaking things for your live domain.

Unfortunately there are no convenient settings or data duplication so configuring your test environment can be a challenge. To help create some dummy data with a little help from Gemini I was able to create the following  createDummyFoldersWithNesting() function:


To use the script

  1. Copy into the Apps Script Editor
  2. Enable the Google Drive Advanced Service
  3. Configure the settings inside the function for the root folder and the number of files/folders to generate

Gemini suggested I highlighted the following key points:

  • Recursion: The createNestedFolders function calls itself to create folders within folders.
  • Randomness: The script randomly determines the number of folders and files to create, and the types of files.
  • Google Apps Script Services: It uses DriveApp and Drive.Files to interact with Google Drive.
  • File Metadata: When creating files, it uses the mimeType property to specify the Google Apps Script file type.

Whilst Gemini wrote a lot of the code for me (with a little guidance), there were a couple of gotchas I’ll highlight.

Drive.Files.create instead of DriveApp.createFile(name, content, mimeType) – the current documentation would suggest that you can use the .createFile() method and include a MimeType like GOOGLE_SHEETS, but as explained in this Stackoverflow post Google have said “After extensive consideration, we have determined that DriveApp.createFile() should not be used to create MimeType.GOOGLE_* files.” 

I could have asked Gemini to rewrite this to use DocumentAppSpreadsheetApp or SlidesApp .create() methods e.g. SpreadsheetApp.create() but then I would have to move into a folder, use extra scopes, which all felt a bit messy so instead opted for Drive.Files.create.

Drive.Files.create supporting Shared Drives without a blob – when using Advanced Services there is a bit of cross referencing required between the auto-complete in the script editor and the associated API documentation. For my script I wanted to support creating files in Shared Drive. To do this requires adding the supportsAllDrives as optionalArgs in the Drive.Files.create(resource, mediaData, optionalArgs) method. As I only wanted blank Docs, Sheets and Slides I was scratching my head as to what to include for the mediaData blob. Fortunately this issue was discussed in the Google Apps Script Community – Google Group and it was clear I could use null or undefined.

Source: Automate Google Drive test data creation with Google Apps Script

How to use Google Apps Script to automate your Gmail out-of-office message

As a Developer Advocate for Google Workspace, I live and breathe productivity tools. But even the most tech-savvy among us can have frustratingly simple oversights. My recurring pain point? Forgetting to turn on my Gmail vacation responder to automatically notify people of my absence before heading out of office.

Google Workspace Developer Advocate, Chanel Greco, has shared a clever solution to automate their Gmail out-of-office notification using Google Apps Script. Faced with the recurring problem of forgetting to set up the out-of-office notification, Chanel crafted a script that does the job based on Google Calendar events marked as ‘outOfOffice’.

Chanel’s project not only solves a personal problem but also demonstrates the benefits of Google Apps Script for task customization and productivity. By harnessing the power of Workspace APIs, you can create innovative solutions to automate routine tasks and streamline workflows.

Follow the source link to read more…

Source: How I Used Apps Script to Never Forget My Gmail Vacation Responder Again

Essential Apps Script: a Practical Guide from the University of York

Welcome to the Essential Apps Script course. This guide is designed so you can work through an introductory course in Google Apps Script, designed so you do not need any knowledge of coding to get started.

As part of our latest episode of Totally Unscripted we chatted to Phil Bainbridge about the work he does at the University of York supporting and creating Google Apps Script projects.

One of the resources the University has developed is the Essential Apps Script course which is designed to lead beginners through the first steps in learning Google Apps Script. The guide is shared under a Creative Commons CC-BY-NC-SA which allows anyone to reuse course materials for non-commercial use if shared with the same licence and attributing the University of York.

This course is a perfect starting point for both beginners and those tasked with helping others learn Google Apps Script. Its step-by-step approach and beginner-friendly language make it easy for anyone with no prior coding experience to grasp the fundamentals. For those supporting others, the course provides a clear and structured resource to guide colleagues through the process, saving valuable time and ensuring everyone is on the same page from the beginning.

Source: Subject Guides: Essential Apps Script: a Practical Guide: Essential Apps Script

Replace text in a Google Doc with an image from Google Drive with Google Apps Script

Search the body of a Google Doc for a specific string/pattern and insert an image in place of it.

In this example the code is designed to sit behind the Google Doc so it is bound to it. There are 4 pieces of information to complete in order to setup the script:

  1. searchText – this is the unique string/pattern in the Doc that you want to replace with an image e.g. “<<keyword>>
  2. imageURL – this is the direct link to the image in Google Drive that you wish to use in the Doc.
  3. size – a numerical value representing the number of pixels for the image’s width/height.
  4. hyperlinkURL – if you want the image to be clickable then provide a link for it.

Source: The Gift of Script: Replace text in a Google Doc with an image

The complete guide to Smart Chips in Google Sheets

Image credit: Ben Collins

Learn how to use Smart Chips in Google Sheets to take full control of your data. Smart chips bring extra information to your Sheets.

Unleash the hidden power of Google Sheets with Smart Chips! Here’s a nice primer for our next episode of Totally Unscripted from Google Sheets magician, Ben Collins. You’ll have to tune in to the show to get the developer angle on Smart Chips, this post instead focusing on out-of-the-box features of Smart Chips in Google Sheets. Ben’s post is still incredible useful and in particular it was very interesting to read about data extraction from Smart Chips including the dot syntax when using Google Sheets formula/functions.

Follow the source link to find out more!

Source: The Complete Guide to Smart Chips in Google Sheets

Post and retrieve messages on Bluesky Social with Google Apps Script

Use Google Apps Script to automate actions on Bluesky Social using their API.

I’ve largely given up on X (formerly Twitter) and there are now numerous alternatives including Bluesky Social. If you would like to automate posting and getting messages from Bluesky Stéphane Giron provides a guide on how to communicate with Bluesky Social API using Google Apps Script. It includes detailed steps on how to obtain an app password, authenticate with Bluesky, retrieve messages for a user, and post a new message. The post also includes code snippets for each step, making it easy if you want to modify this integration to your own needs.

Source: Post and Get messages with Bluesky Social API and Google Apps Script

Manage Google Form onFormSubmit script executions with Script Lock

Use the Apps Script Lock Service to control Form submissions and prevent data loss

Lock Service code snippet

Lock Service code snippet

The following Google Apps Script is a one example of how the Lock Service can be used to prevent concurrent running of code. Here we have a Google Form that can be submitted by users at any point, the code then takes some of those details and appends them to another Google Sheet row. In normal circumstances this will happen relatively quickly and without clashes, but what if multiple people submit the Form at the same time!?

[Editor note: An alternative approach to tryLock() is waitLock(). The only different with a waitLock() is it will throw an exception after the set number of milliseconds. An example of waitLock() with onFormSubmit is included in the reference documentation]

Source: The Gift of Script: Control Form submissions with Script Lock

How you can use Google Forms, AI, and Apps Script automation to analyze 1,700 survey responses (and the 1,000th AppsScriptPulse post)

This post describes how I designed and ran an audience survey with over 1,700 responses, using Google Forms, Sheets, Apps Script, and ChatGPT. I’ll show you the entire process from end-to-end, including how I:

  • Created a survey with Google Forms
  • Used Apps Script to automatically say thank you to 1,700 respondents
  • Analyzed the response data in Google Sheets
  • Used AI to help me understand the qualitative data
  • Presented the results in Google Docs

It’s rather fitting that the 1,000th Pulse post features content by the one and only Ben Collins! Back in late 2019, when I was thinking about creating a new community site for Google Workspace developers, Ben’s encouragement was the spark that ignited AppsScriptPulse.

And today’s post by Ben is a nice example of Apps Script’s power to automate repetitive tasks. As part of this he shows how to craft personalised “thank you” emails for Google Form survey response with Google Apps Script. Ben’s insights go beyond ‘thank-you’s as he outlines how he administers and analyses customer surveys, highlighting his design choices for Google Forms and data analysis using built-in Google Sheets functions.

To take things a step further, Ben also highlights how he used ChatGPT to categorize qualitative survey responses. With Google’s recent announcement of their new AI model, Gemini, which outperforms ChatGPT  in a number of academic benchmarks, it would be interesting to see how these two platforms compare for this type of analysis.

Raising a glass (or an espresso :) to Ben and this 1,000-post milestone!

Source: How To Analyze Google Forms Survey Data with AI and Apps Script