Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active January 12, 2023 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tanaikech/4700361cc060ae4333672da905d272c7 to your computer and use it in GitHub Desktop.
Save tanaikech/4700361cc060ae4333672da905d272c7 to your computer and use it in GitHub Desktop.
Changing Font of Selected Text to 'Google Sans' on Google Document using Google Apps Script

Changing Font of Selected Text to 'Google Sans' on Google Document using Google Apps Script

This is a sample script for changing the font of selected text to Google Sans on Google Document using Google Apps Script.

Sample script

Please copy and paste the following script to the script editor of Google Document And, when you use this script, please select a text in Google Document and run the script. By this, the font of selected text is changed to Google Sans.

function myFunction() {
  const selection = DocumentApp.getActiveDocument().getSelection();
  const element = selection.getRangeElements()[0];
  element
    .getElement()
    .asText()
    .setFontFamily(
      element.getStartOffset(),
      element.getEndOffsetInclusive(),
      "Google Sans"
    );
}

References

This sample script was posted for explaining at https://groups.google.com/g/google-apps-script-community/c/ehC7C95W5t0.

Testing

  • March 6, 2022: I confirmed that this sample script worked fine.
@WillDev12
Copy link

Very nice, thank you for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment