Below is a Google Apps Script code snippet that sets the font of a paragraph in a Google Doc to "Spectral Light." Note that "Spectral Light" is likely referring to the Spectral font family with a light weight variant. However, in Google Docs Apps Script, you can only set the font family directly (e.g., "Spectral"). If "Light" is a specific weight, it may not be directly controllable via the API, as font weights like "Light" are part of the font's variants and depend on what's available in Google Docs. Setting the font family to "Spectral" should apply the appropriate variant if it exists.
This script will apply the font to all paragraphs in the document for simplicity. If you want to target a specific paragraph, you can modify the code accordingly.
Instructions:
- Open your Google Doc.
- Go to Extensions > Apps Script to open the Apps Script editor.
- Paste the code into the editor.
- Save the script with a name (e.g., "SetFontScript").
- Run the function (e.g.,
setParagraphFontToSpectralLight) from the editor or set up a trigger.
Code:
function setParagraphFontToSpectralLight() {
// Get the active Google Doc
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs(); // Get all paragraphs in the document
// Loop through each paragraph and set the font family
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
var text = paragraph.asText(); // Get the text element of the paragraph
if (text && text.getText().length > 0) { // Ensure the paragraph has text
var length = text.getText().length; // Get the length of the text
text.setFontFamily(0, length, 'Spectral'); // Set the font family to 'Spectral'
// Note: If 'Spectral Light' is intended as a single string, you can try it here,
// but it may not work as expected. Use 'Spectral' for reliability.
}
}
// Optional: Show a toast notification to confirm the changes
doc.toast('Font set to Spectral for all paragraphs.');
}
Explanation:
-
Line by Line Breakdown:
var doc = DocumentApp.getActiveDocument();: Gets the currently open document.
var body = doc.getBody();: Accesses the main body of the document.
var paragraphs = body.getParagraphs();: Retrieves an array of all paragraphs in the body.
- The loop iterates through each paragraph.
var text = paragraph.asText();: Converts the paragraph to a Text object, allowing us to manipulate text attributes like font.
text.setFontFamily(0, length, 'Spectral');: Sets the font family for the entire text of the paragraph. The parameters are:
0: Starting index (beginning of the text).
length: Ending index (end of the text).
'Spectral': The font family name.
- If you want to set other attributes (e.g., font size), you can add them like this:
text.setFontSize(0, length, 12); for a size of 12pt.
-
Potential Issues:
- If "Spectral Light" isn't a recognized font family in Google Docs, the script will default to "Spectral" or another fallback font. You can test this by running the script and checking the document.
- Font weights (e.g., Light, Bold) aren't directly settable via
setFontFamily(). If you need a specific weight, you might have to rely on Google Docs' built-in font rendering or use setBold(true/false) for basic adjustments.
- This script modifies all paragraphs. To target just the first paragraph, change the loop to:
var paragraph = paragraphs[0]; and apply the changes only to that one.
If this doesn't achieve exactly what you want (e.g., if you meant to modify a named style like "Normal Text"), let me know more details, and I can refine the code!