0% found this document useful (0 votes)
9 views36 pages

Essential Apps Script Snippets for Docs

Uploaded by

yiyimip426
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views36 pages

Essential Apps Script Snippets for Docs

Uploaded by

yiyimip426
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

100 Useful Apps Script Code Snippets

Google Apps Script is a powerful way to automate, customize, and supercharge your workflow
in Google Docs. Whether you want to clean up references, format your document, or do
advanced text processing, a simple script can save you hours. Here are 10 essential Google
Apps Script snippets every Google Docs power user should know!

1. Remove All [cite: 1234] References

Description:​
This script removes every instance of [cite: 1234] (where the number can be anything)
from your document.

function removeCiteReferences() {
var body = [Link]().getBody();
var pattern = /\[cite:\s*\d+\]/g;
var text = [Link]();
var cleanedText = [Link](pattern, '');
[Link](cleanedText);
}

2. Convert All “Exercise” Sections to Heading 2

Description:​
Automatically converts every paragraph starting with "Exercise <number>:" to the Heading 2
style.

function convertExercisesToHeading2() {
var body = [Link]().getBody();
var totalParas = [Link]();

for (var i = 0; i < totalParas; i++) {


var element = [Link](i);
if ([Link]() == [Link]) {
var para = [Link]();

Laurence Svekis Learn More [Link]


var text = [Link]();
if (/^Exercise\s+\d+:/[Link](text)) {
[Link]([Link].HEADING2);
}
}
}
}

3. Find and Highlight All TODOs

Description:​
Highlights every “TODO” in yellow so you can spot unfinished work at a glance.

function highlightTODOs() {
var body = [Link]().getBody();
var found = [Link]('TODO');
while (found) {
[Link]().setBackgroundColor('yellow');
found = [Link]('TODO', found);
}
}

4. Replace All Instances of a Word

Description:​
Find and replace every instance of a specific word or phrase.

function replaceWord(oldWord, newWord) {


var body = [Link]().getBody();
[Link](oldWord, newWord);
}
// Example usage: replaceWord('foo', 'bar');

5. Add a Table of Contents at the Top

Laurence Svekis Learn More [Link]


Description:​
Inserts an up-to-date Table of Contents at the beginning of your document.

function insertTableOfContents() {
var body = [Link]().getBody();
[Link](0, "Table of
Contents").setHeading([Link].HEADING1);
[Link](1, [Link]);
}

6. Remove Extra Blank Lines

Description:​
Deletes consecutive blank paragraphs for a cleaner document.

function removeExtraBlankLines() {
var body = [Link]().getBody();
var numChildren = [Link]();
for (var i = numChildren - 1; i > 0; i--) {
var curr = [Link](i);
var prev = [Link](i - 1);
if ([Link]() == [Link] &&
[Link]() == [Link] &&
![Link]().getText() &&
![Link]().getText()) {
[Link](curr);
}
}
}

7. List All Images with Their Index

Description:​
Prints a log of all images in your document with their position number.

Laurence Svekis Learn More [Link]


function listAllImages() {
var body = [Link]().getBody();
var imgs = [Link]();
for (var i = 0; i < [Link]; i++) {
[Link]("Image " + (i + 1) + " found.");
}
}

8. Count Words in Your Document

Description:​
Counts and logs the number of words in your document.

function countWords() {
var text = [Link]().getBody().getText();
var wordCount = [Link](/\b\S+\b/g)?.length || 0;
[Link]('Total words: ' + wordCount);
}

9. Change All Headings to Title Case

Description:​
Automatically updates all headings (H1, H2, H3, etc.) to Title Case.

function headingsToTitleCase() {
var body = [Link]().getBody();
var total = [Link]();
for (var i = 0; i < total; i++) {
var elem = [Link](i);
if ([Link]() == [Link]) {
var para = [Link]();
var heading = [Link]();
if (heading != [Link]) {
var txt = [Link]();

Laurence Svekis Learn More [Link]


var newTxt = [Link](/\w\S*/g, w =>
[Link](0).toUpperCase() + [Link](1).toLowerCase());
[Link](newTxt);
}
}
}
}

10. Insert the Current Date Anywhere

Description:​
Quickly insert today’s date wherever your cursor is in the doc.

function insertCurrentDate() {
var cursor = [Link]().getCursor();
if (cursor) {
var dateStr = [Link](new Date(),
[Link](), "yyyy-MM-dd");
[Link](dateStr);
} else {
[Link]().alert('Place your cursor in the document
first.');
}
}

How to Use These Snippets


1.​ Open your Google Doc.​

2.​ Go to Extensions → Apps Script.​

3.​ Paste your chosen script(s) into the code editor.​

4.​ Save and run the function you want (authorize if prompted).​

Laurence Svekis Learn More [Link]


11. Duplicate the Active Document

function duplicateActiveDoc() {
var doc = [Link]();
[Link]([Link]()).makeCopy('Copy of ' +
[Link]());
}

12. Get a List of All Headings

function listAllHeadings() {
var body = [Link]().getBody();
var numChildren = [Link]();
for (var i = 0; i < numChildren; i++) {
var elem = [Link](i);
if ([Link]() == [Link]) {
var para = [Link]();
if ([Link]() !== [Link]) {
[Link]([Link]());
}
}
}
}

13. Export Document as PDF and Email

function emailAsPDF() {
var doc = [Link]();
var file = [Link]([Link]());
var pdf = [Link]('application/pdf');
[Link]('your@[Link]', 'Document PDF', 'See attached.',
{attachments:[pdf]});
}

Laurence Svekis Learn More [Link]


14. Remove All Hyperlinks

function removeAllHyperlinks() {
var body = [Link]().getBody();
var search = [Link]('https?://[^\s]+', null, {matchCase:
false, wholeWord: false, useRegularExpression: true});
while (search) {
[Link]().setLinkUrl(null);
search = [Link]('https?://[^\s]+', search,
{useRegularExpression: true});
}
}

15. Capitalize All Paragraphs

function capitalizeAllParagraphs() {
var body = [Link]().getBody();
var n = [Link]();
for (var i = 0; i < n; i++) {
var el = [Link](i);
if ([Link]() == [Link]) {
var txt = [Link]().getText();
[Link]().setText([Link]());
}
}
}

16. Count All Images

function countImages() {
var body = [Link]().getBody();
[Link]('Image count: ' + [Link]().length);
}

Laurence Svekis Learn More [Link]


17. Convert All Text to Arial

function convertAllTextToArial() {
var body = [Link]().getBody();
[Link]().setFontFamily('Arial');
}

18. Remove All Comments

function removeAllComments() {
var doc = [Link]();
var comments = [Link]([Link]()).items;
[Link](function(comment) {
[Link]([Link](), [Link]);
});
}

19. Insert a Footer

function insertFooter() {
var doc = [Link]();
[Link]().appendParagraph("Confidential");
}

20. Insert a Header

function insertHeader() {
var doc = [Link]();
[Link]().appendParagraph("Draft Version");
}

21. Highlight All Email Addresses

Laurence Svekis Learn More [Link]


function highlightEmails() {
var body = [Link]().getBody();
var found =
[Link]('[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,}', null,
{useRegularExpression:true});
while (found) {
[Link]().setBackgroundColor('lightblue');
found =
[Link]('[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,}', found,
{useRegularExpression:true});
}
}

22. Make All Paragraphs Double-Spaced

function doubleSpaceParagraphs() {
var body = [Link]().getBody();
var n = [Link]();
for (var i = 0; i < n; i++) {
var p = [Link](i);
if ([Link]() == [Link]) {
[Link]().setLineSpacing(2);
}
}
}

23. Add Page Numbers to Footer

function addPageNumbersToFooter() {
var doc = [Link]();
var footer = [Link]();
[Link]();
}

Laurence Svekis Learn More [Link]


24. Insert Image by URL

function insertImageByUrl() {
var url =
'[Link]
_272x92dp.png';
var imgBlob = [Link](url).getBlob();
[Link]().getBody().appendImage(imgBlob);
}

25. Find and Bold All Numbers

function boldAllNumbers() {
var body = [Link]().getBody();
var found = [Link]('\\d+', null, {useRegularExpression:
true});
while (found) {
[Link]().setBold(true);
found = [Link]('\\d+', found, {useRegularExpression:
true});
}
}

26. List All Bookmarks

function listBookmarks() {
var doc = [Link]();
var bookmarks = [Link]();
[Link](function(bm, i) {
[Link]('Bookmark ' + (i+1) + ': ' + [Link]());
});
}

Laurence Svekis Learn More [Link]


27. Remove All Bookmarks

function removeBookmarks() {
var doc = [Link]();
[Link]().forEach(function(bm) { [Link](); });
}

28. Merge All Tables into One

function mergeAllTables() {
var body = [Link]().getBody();
var tables = [];
for (var i = 0; i < [Link](); i++) {
if ([Link](i).getType() == [Link]) {
[Link]([Link](i).asTable());
}
}
if ([Link] < 2) return;
var mainTable = tables[0];
for (var i = 1; i < [Link]; i++) {
var table = tables[i];
for (var r = 0; r < [Link](); r++) {
[Link]([Link](r).copy());
}
[Link](table);
}
}

29. Count All Tables

function countTables() {
var body = [Link]().getBody();
var count = 0;

Laurence Svekis Learn More [Link]


for (var i = 0; i < [Link](); i++) {
if ([Link](i).getType() == [Link])
count++;
}
[Link]("Table count: " + count);
}

30. Add “Draft” Watermark to Each Page (Header)

function draftHeader() {
var doc = [Link]();
[Link]().appendParagraph("DRAFT");
}

31. Center Align All Headings

function centerAlignHeadings() {
var body = [Link]().getBody();
var n = [Link]();
for (var i = 0; i < n; i++) {
var elem = [Link](i);
if ([Link]() == [Link]) {
var para = [Link]();
if ([Link]() !== [Link]) {
[Link]([Link]);
}
}
}
}

32. Remove All Bold Formatting

function removeAllBold() {

Laurence Svekis Learn More [Link]


var text = [Link]().getBody().editAsText();
[Link](false);
}

33. List All Links

function listAllLinks() {
var body = [Link]().getBody();
var text = [Link]();
var matches = [Link](/https?:\/\/[^\s]+/g);
if (matches) [Link]([Link]('\n'));
}

34. Insert a Horizontal Line

function insertHorizontalLine() {
[Link]().getBody().appendHorizontalRule();
}

35. Add “Reviewed” to Document Title

function markReviewedInTitle() {
var doc = [Link]();
[Link]([Link]() + ' (Reviewed)');
}

36. Replace All Numbers with “#”

function replaceAllNumbers() {
var body = [Link]().getBody();
[Link]([Link]().replace(/\d+/g, '#'));
}

Laurence Svekis Learn More [Link]


37. Add Indentation to All Paragraphs

function indentAllParagraphs() {
var body = [Link]().getBody();
var n = [Link]();
for (var i = 0; i < n; i++) {
var para = [Link](i);
if ([Link]() == [Link]) {
[Link]().setIndentStart(36); // Half-inch
}
}
}

38. Remove All Indentation

function removeAllIndentation() {
var body = [Link]().getBody();
var n = [Link]();
for (var i = 0; i < n; i++) {
var para = [Link](i);
if ([Link]() == [Link]) {
[Link]().setIndentStart(0);
}
}
}

39. Append Text at the End

function appendTextEnd() {
[Link]().getBody().appendParagraph('--- End
of Document ---');
}

Laurence Svekis Learn More [Link]


40. Insert Current User’s Email

function insertCurrentUserEmail() {
var email = [Link]().getEmail();
[Link]().getBody().appendParagraph(email);
}

41. List All Unique Words

function listUniqueWords() {
var text = [Link]().getBody().getText();
var words = [Link](/\b\w+\b/g);
var unique = [Link](new Set(words));
[Link]([Link](', '));
}

42. Delete All Tables

function deleteAllTables() {
var body = [Link]().getBody();
for (var i = [Link]() - 1; i >= 0; i--) {
if ([Link](i).getType() == [Link]) {
[Link]([Link](i));
}
}
}

43. Add Highlight to All Headings

function highlightAllHeadings() {
var body = [Link]().getBody();

Laurence Svekis Learn More [Link]


var n = [Link]();
for (var i = 0; i < n; i++) {
var el = [Link](i);
if ([Link]() == [Link]) {
var para = [Link]();
if ([Link]() !== [Link]) {
[Link]('#ffe599');
}
}
}
}

44. Find and Italicize All Quoted Text

function italicizeQuotes() {
var body = [Link]().getBody();
var found = [Link]('"[^"]+"', null,
{useRegularExpression:true});
while (found) {
[Link]().setItalic(true);
found = [Link]('"[^"]+"', found,
{useRegularExpression:true});
}
}

45. Add a Hyperlink to a Selected Word

function addHyperlinkToSelection(url) {
var selection = [Link]().getSelection();
if (selection) {
var elements = [Link]();
[Link](function(el) {
var elem = [Link]().asText();
[Link]([Link](), [Link](),
url);

Laurence Svekis Learn More [Link]


});
}
}
// Example usage: addHyperlinkToSelection('[Link]

46. Replace All Tab Characters with Spaces

function replaceTabsWithSpaces() {
var body = [Link]().getBody();
var txt = [Link]().replace(/\t/g, ' ');
[Link](txt);
}

47. Remove All Underlines

function removeAllUnderlines() {
var text = [Link]().getBody().editAsText();
[Link](false);
}

48. List Paragraphs Longer Than 100 Characters

function longParagraphs() {
var body = [Link]().getBody();
var n = [Link]();
for (var i = 0; i < n; i++) {
var el = [Link](i);
if ([Link]() == [Link]) {
var txt = [Link]().getText();
if ([Link] > 100) [Link](txt);
}
}
}

Laurence Svekis Learn More [Link]


49. Set All Text Color to Black

function setAllTextBlack() {
var body = [Link]().getBody().editAsText();
[Link]('black');
}

50. Make All Headings Bold

function makeHeadingsBold() {
var body = [Link]().getBody();
var n = [Link]();
for (var i = 0; i < n; i++) {
var el = [Link](i);
if ([Link]() == [Link]) {
var para = [Link]();
if ([Link]() !== [Link]) {
[Link](true);
}
}
}
}

51. Log All Paragraph Styles

function logParagraphStyles() {
var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
var p = [Link](i);
if ([Link]() == [Link]) {
[Link]([Link]().getHeading());
}

Laurence Svekis Learn More [Link]


}
}

52. Set Document Font Size to 12

function setFontSize12() {
var text = [Link]().getBody().editAsText();
[Link](12);
}

53. Remove All Strikethrough

function removeAllStrikethrough() {
var text = [Link]().getBody().editAsText();
[Link](false);
}

54. Highlight All Paragraphs Containing “Important”

function highlightImportant() {
var body = [Link]().getBody();
var n = [Link]();
for (var i = 0; i < n; i++) {
var el = [Link](i);
if ([Link]() == [Link]) {
if ([Link]().getText().match(/important/i)) {
[Link]().setBackgroundColor('pink');
}
}
}
}

Laurence Svekis Learn More [Link]


55. Add Table with 3 Rows and 3 Columns

function add3x3Table() {
var body = [Link]().getBody();

[Link]([['A1','B1','C1'],['A2','B2','C2'],['A3','B3','C3']])
;
}

56. Remove All Tables Except the First

function keepFirstTableOnly() {
var body = [Link]().getBody();
var foundFirst = false;
for (var i = [Link]() - 1; i >= 0; i--) {
var child = [Link](i);
if ([Link]() == [Link]) {
if (!foundFirst) {
foundFirst = true;
} else {
[Link](child);
}
}
}
}

57. Add Timestamp to End of Doc

function addTimestampEnd() {
var now = new Date();
var str = [Link](now, [Link](),
'yyyy-MM-dd HH:mm:ss');
[Link]().getBody().appendParagraph(str);
}

Laurence Svekis Learn More [Link]


58. Highlight All Numbers Over 100

function highlightLargeNumbers() {
var body = [Link]().getBody();
var found = [Link]('\\b\\d{3,}\\b', null,
{useRegularExpression:true});
while (found) {
[Link]().setBackgroundColor('#f4cccc');
found = [Link]('\\b\\d{3,}\\b', found,
{useRegularExpression:true});
}
}

59. Insert Line Breaks After Every Sentence

function breakAfterSentences() {
var body = [Link]().getBody();
var text = [Link]().replace(/([.?!])\s/g, '$1\n');
[Link](text);
}

60. Add “Confidential” Watermark (Footer)

function addConfidentialFooter() {

[Link]().addFooter().appendParagraph("CONFIDENT
IAL");
}

61. Set All Paragraphs to Single Spacing

Laurence Svekis Learn More [Link]


function singleSpacing() {
var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
var p = [Link](i);
if ([Link]() == [Link]) {
[Link]().setLineSpacing(1);
}
}
}

62. Find All Paragraphs Starting with “Note:”

function findNotes() {
var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
var para = [Link](i);
if ([Link]() == [Link]) {
var text = [Link]().getText();
if (/^Note:/[Link](text)) [Link](text);
}
}
}

63. Convert All Text to Sentence Case

function toSentenceCase() {
var body = [Link]().getBody();
var text =
[Link]().toLowerCase().replace(/(^\s*\w|[.!?]\s*\w)/g,
function(c){return [Link]()});
[Link](text);
}

Laurence Svekis Learn More [Link]


64. Insert a Blank Page at End

function insertBlankPage() {
var body = [Link]().getBody();
[Link]();
}

65. Insert a Table of Figures Placeholder

function insertTableOfFigures() {
var body = [Link]().getBody();
[Link](0, "Table of
Figures").setHeading([Link].HEADING1);
}

66. Add a Horizontal Line After Every Heading

function lineAfterHeading() {
var body = [Link]().getBody();
for (var i = [Link]() - 1; i >= 0; i--) {
var para = [Link](i);
if ([Link]() == [Link] &&
[Link]().getHeading() !=
[Link]) {
[Link](i+1);
}
}
}

67. Highlight All Unique Words Longer Than 10 Letters

function highlightLongUniqueWords() {
var body = [Link]().getBody();

Laurence Svekis Learn More [Link]


var text = [Link]();
var longWords = [Link](new Set([Link](/\b\w{11,}\b/g) ||
[]));
[Link](function(word) {
var found = [Link]('\\b'+word+'\\b');
while (found) {
[Link]().setBackgroundColor('#d9ead3');
found = [Link]('\\b'+word+'\\b', found);
}
});
}

68. Remove All Images

function removeAllImages() {
var body = [Link]().getBody();
var imgs = [Link]();
for (var i = [Link] - 1; i >= 0; i--) {
var parent = imgs[i].getParent();
if (parent) [Link](imgs[i]);
}
}

69. Add a Cover Page

function addCoverPage() {
var body = [Link]().getBody();
[Link](0, "My Document
Title").setHeading([Link]);
[Link](1, "Author
Name").setHeading([Link].HEADING2);
[Link](2,
"").setHeading([Link]);
[Link](3);
}

Laurence Svekis Learn More [Link]


70. Randomize Paragraph Order

function randomizeParagraphs() {
var body = [Link]().getBody();
var paras = [];
for (var i = 0; i < [Link](); i++) {
var el = [Link](i);
if ([Link]() == [Link]) {
[Link]([Link]().copy());
}
}
for (var i = [Link]() - 1; i >= 0; i--) {
if ([Link](i).getType() ==
[Link])
[Link]([Link](i));
}
[Link](function(){return [Link]()});
[Link](function(para) { [Link]([Link]());
});
}

71. Replace All “lorem” With “ipsum”

function replaceLoremWithIpsum() {
var body = [Link]().getBody();
[Link]('lorem', 'ipsum');
}

72. List All Paragraphs Containing a Number

function paragraphsWithNumber() {
var body = [Link]().getBody();

Laurence Svekis Learn More [Link]


for (var i = 0; i < [Link](); i++) {
var p = [Link](i);
if ([Link]() == [Link]) {
var txt = [Link]().getText();
if (/\d/.test(txt)) [Link](txt);
}
}
}

73. Delete All Paragraphs Containing “delete me”

function deleteParagraphsWithDeleteMe() {
var body = [Link]().getBody();
for (var i = [Link]() - 1; i >= 0; i--) {
var para = [Link](i);
if ([Link]() == [Link]) {
if (/delete me/[Link]([Link]().getText()))
[Link](para);
}
}
}

74. Make All Links Blue

function setAllLinksBlue() {
var body = [Link]().getBody();
var found = [Link]('https?://[^\s]+', null,
{useRegularExpression:true});
while (found) {
[Link]().setForegroundColor('blue');
found = [Link]('https?://[^\s]+', found,
{useRegularExpression:true});
}
}

Laurence Svekis Learn More [Link]


75. Number All Headings Sequentially

function numberHeadings() {
var body = [Link]().getBody();
var n = [Link]();
var count = 1;
for (var i = 0; i < n; i++) {
var el = [Link](i);
if ([Link]() == [Link]) {
var para = [Link]();
if ([Link]() !== [Link]) {
[Link](count + ". " + [Link]());
count++;
}
}
}
}

76. Highlight Palindromes

function highlightPalindromes() {
var body = [Link]().getBody();
var text = [Link]();
var words = [Link](/\b\w+\b/g) || [];
[Link](function(word) {
if ([Link] > 2 && word === [Link]('').reverse().join(''))
{
var found = [Link]('\\b' + word + '\\b');
while (found) {
[Link]().setBackgroundColor('#d9ead3');
found = [Link]('\\b' + word + '\\b', found);
}
}
});
}

Laurence Svekis Learn More [Link]


77. Log Document Creation Date

function logDocCreationDate() {
var doc = [Link]();
var file = [Link]([Link]());
[Link]([Link]());
}

78. Remove All Formatting

function removeAllFormatting() {
var body = [Link]().getBody();
var text = [Link]();
[Link](text); // Removes all formatting
}

79. Duplicate the First Paragraph

function duplicateFirstParagraph() {
var body = [Link]().getBody();
var first = [Link](0);
if ([Link]() == [Link]) {
[Link](1, [Link]().getText());
}
}

80. Move All Headings to Top

function moveHeadingsToTop() {
var body = [Link]().getBody();
var headings = [];

Laurence Svekis Learn More [Link]


for (var i = [Link]() - 1; i >= 0; i--) {
var para = [Link](i);
if ([Link]() == [Link] &&
[Link]().getHeading() !==
[Link]) {
[Link]([Link]().getText());
[Link](para);
}
}
[Link](function(h, idx) {
[Link](idx,
h).setHeading([Link].HEADING1);
});
}

81. Insert Blank Paragraph After Each Heading

function blankAfterHeadings() {
var body = [Link]().getBody();
for (var i = [Link]() - 1; i >= 0; i--) {
var para = [Link](i);
if ([Link]() == [Link] &&
[Link]().getHeading() !==
[Link]) {
[Link](i + 1, '');
}
}
}

82. Replace All Exclamation Marks With Periods

function exclamationsToPeriods() {
var body = [Link]().getBody();
var text = [Link]().replace(/!/g, '.');
[Link](text);

Laurence Svekis Learn More [Link]


}

83. Add “Summary” Section at End

function addSummarySection() {
var body = [Link]().getBody();

[Link]('Summary').setHeading([Link]
g.HEADING1);
}

84. Find and Bold All Proper Nouns

function boldProperNouns() {
var body = [Link]().getBody();
var found = [Link]('\\b[A-Z][a-z]+\\b', null,
{useRegularExpression:true});
while (found) {
[Link]().setBold(true);
found = [Link]('\\b[A-Z][a-z]+\\b', found,
{useRegularExpression:true});
}
}

85. Insert Document Word Count at the Top

function insertWordCountAtTop() {
var body = [Link]().getBody();
var count = [Link]().match(/\b\S+\b/g)?.length || 0;
[Link](0, 'Word Count: ' + count);
}

Laurence Svekis Learn More [Link]


86. Insert a Divider Line After Each Table

function lineAfterEachTable() {
var body = [Link]().getBody();
for (var i = [Link]() - 1; i >= 0; i--) {
var el = [Link](i);
if ([Link]() == [Link]) {
[Link](i + 1);
}
}
}

87. Log All Paragraph Indentations

function logIndentations() {
var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
var p = [Link](i);
if ([Link]() == [Link]) {
[Link]([Link]().getIndentStart());
}
}
}

88. Remove All Empty Headings

function removeEmptyHeadings() {
var body = [Link]().getBody();
for (var i = [Link]() - 1; i >= 0; i--) {
var para = [Link](i);
if ([Link]() == [Link] &&
[Link]().getHeading() !==
[Link] &&
![Link]().getText().trim()) {
[Link](para);

Laurence Svekis Learn More [Link]


}
}
}

89. Highlight All Dates (YYYY-MM-DD)

function highlightDates() {
var body = [Link]().getBody();
var found = [Link]('\\b\\d{4}-\\d{2}-\\d{2}\\b', null,
{useRegularExpression:true});
while (found) {
[Link]().setBackgroundColor('#fce5cd');
found = [Link]('\\b\\d{4}-\\d{2}-\\d{2}\\b', found,
{useRegularExpression:true});
}
}

90. List All Tables and Their Size

function logTableSizes() {
var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
if ([Link](i).getType() == [Link]) {
var table = [Link](i).asTable();
[Link]('Table at index ' + i + ': ' + [Link]() + '
rows x ' + [Link](0).getNumCells() + ' cols');
}
}
}

91. Make All Text 1.5 Line Spaced

function onePointFiveSpacing() {

Laurence Svekis Learn More [Link]


var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
var p = [Link](i);
if ([Link]() == [Link]) {
[Link]().setLineSpacing(1.5);
}
}
}

92. Highlight All Questions

function highlightQuestions() {
var body = [Link]().getBody();
var found = [Link]('[^?]+\?', null,
{useRegularExpression:true});
while (found) {
[Link]().setBackgroundColor('#d9ead3');
found = [Link]('[^?]+\?', found,
{useRegularExpression:true});
}
}

93. Insert an Index of All Headings at Top

function insertHeadingsIndex() {
var body = [Link]().getBody();
var headings = [];
for (var i = 0; i < [Link](); i++) {
var para = [Link](i);
if ([Link]() == [Link] &&
[Link]().getHeading() !==
[Link]) {
[Link]([Link]().getText());
}
}

Laurence Svekis Learn More [Link]


[Link](0, "Index: " + [Link](" | "));
}

94. Log All Paragraph Alignments

function logAlignments() {
var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
var p = [Link](i);
if ([Link]() == [Link]) {
[Link]([Link]().getAlignment());
}
}
}

95. Convert All Uppercase Words to Lowercase

function uppercaseToLowercase() {
var body = [Link]().getBody();
var text = [Link]().replace(/\b[A-Z]{2,}\b/g,
function(w){return [Link]();});
[Link](text);
}

96. Highlight All Headings with Custom Color

function highlightHeadingsColor() {
var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
var para = [Link](i);
if ([Link]() == [Link] &&
[Link]().getHeading() !==
[Link]) {

Laurence Svekis Learn More [Link]


[Link]().setBackgroundColor('#b6d7a8');
}
}
}

97. List All Section Breaks

function logSectionBreaks() {
var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
var el = [Link](i);
if ([Link]() == [Link].SECTION_BREAK) {
[Link]('Section break at index ' + i);
}
}
}

98. Highlight All Capitalized Words

function highlightCapWords() {
var body = [Link]().getBody();
var found = [Link]('\\b[A-Z][A-Z]+\\b', null,
{useRegularExpression:true});
while (found) {
[Link]().setBackgroundColor('#f4cccc');
found = [Link]('\\b[A-Z][A-Z]+\\b', found,
{useRegularExpression:true});
}
}

99. Remove All Paragraph Background Colors

function clearAllBackgroundColors() {

Laurence Svekis Learn More [Link]


var body = [Link]().getBody();
for (var i = 0; i < [Link](); i++) {
var para = [Link](i);
if ([Link]() == [Link]) {
[Link]().setBackgroundColor(null);
}
}
}

100. Add "Appendix" Section at End

function addAppendixSection() {
var body = [Link]().getBody();

[Link]('Appendix').setHeading([Link]
ng.HEADING1);
}

Laurence Svekis Learn More [Link]

You might also like