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

Gists

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.