Skip to content

Instantly share code, notes, and snippets.

@geoffreycrofte
Last active November 26, 2022 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoffreycrofte/b590b370e87e7747177cbb858a45c42d to your computer and use it in GitHub Desktop.
Save geoffreycrofte/b590b370e87e7747177cbb858a45c42d to your computer and use it in GitHub Desktop.
Google Docs: Numbering Document headings (titles) Script
/**!
* NUMBER THE HEADINGS
* This script can't be used for commercial purpose.
*
* Script improved by: Geoffrey Crofte / GeoffreyCrofte.com / 2022
* Inspired by: Antoine Martin / ThierrryVanoffe.com / 2020
*/
// Constants will be used later on.
const ui = DocumentApp.getUi();
const thisDoc = DocumentApp.getActiveDocument();
const body = thisDoc.getBody();
const blocks = body.getParagraphs();
// Regular expression to use for the removing function.
// Uses pipes to consider conditions "or".
const pattern = "(([0-9]{1,3}\.){1}|([0-9]{1,3}\.){1}([0-9]{1,3}\.){1}|([0-9]{1,3}\.){1}([0-9]{1,3}\.){1}([a-z]{1}\.{1})|([0-9]{1,3}\.){1}([0-9]{1,3}\.){1}([0-9]{1,3}\.){1}|([0-9]{1,3}\.){1}([0-9]{1,3}\.){1}([0-9]{1,3}\.){1}([a-z]{1}\.){1})[ ]{1}";
/**
* When the document is opened by the user, set the menu into the UI.
* @see: https://developers.google.com/apps-script/guides/menus
*/
function onOpen() {
let ui = DocumentApp.getUi();
ui.createMenu("🔧Tools")
.addSubMenu(ui.createMenu("Number the headings")
.addItem("Number headings 1 to 2", "numberHeadings12")
.addItem("Number headings 1 to 3", "numberHeadings13")
.addItem("Number headings 1 to 4", "numberHeadings14")
.addSeparator()
.addItem("Remove numbering","removeNumbering")
)
.addToUi();
}
/**
* Default aliased function triggered to number the headings.
* @param: to (int) the maximum level numbered. (2 to 4)
*/
function numberHeadings(to) {
//Supprimer tous les numéros d'une numérotation précédente
removeNumbering();
// Counters start at 0 since we will increment them firts.
let nh1 = 0;
let nh2 = 0;
let nh3 = 0;
let nh4 = 0;
let alpha = ["","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
// if to (max heading level) isn't set, take 3 by default.
to = to ? parseInt(to) : 3;
// Loop on all the block types (p, hn, ul, li)
for (let i = 0 ; i < blocks.length ; i++) {
let thisBlock = blocks[i];
let heading = thisBlock.getHeading();
switch ( heading ) {
// Heading 1
case DocumentApp.ParagraphHeading.HEADING1:
// Increment H1 and re-init subsequent headings.
nh1++;
nh2 = 0;
nh3 = 0;
nh4 = 0;
// Insert the number before the text with a ". " between.
thisBlock.editAsText().insertText(0 , nh1+". ");
break;
// Heading 2
case DocumentApp.ParagraphHeading.HEADING2:
nh2++;
nh3 = 0;
nh4 = 0;
thisBlock.editAsText().insertText(0 , nh1+"."+nh2+". ");
break;
// Heading 3
case DocumentApp.ParagraphHeading.HEADING3:
if ( to === 2 ) break;
nh3++;
nh4 = 0;
// Condition here, alpha if heading til 3, else it's a number.
thisBlock.editAsText().insertText(0, nh1+"."+nh2+"." + (to === 3 ? alpha[nh3] : nh3) + ". ");
break;
// Heading 4
case DocumentApp.ParagraphHeading.HEADING4:
if ( to <= 3 ) break;
nh4++;
thisBlock.editAsText().insertText(0, nh1+"."+nh2+"."+nh3+"."+alpha[nh4]+". ");
break;
}
}
}
/**
* Function that removes the numbering regarding the pattern declared.
*/
function removeNumbering() {
for (let i = 0 ; i < blocks.length ; i++) {
let thisBlock = blocks[i];
let heading = thisBlock.getHeading();
if (
heading == DocumentApp.ParagraphHeading.HEADING4 ||
heading == DocumentApp.ParagraphHeading.HEADING3 ||
heading == DocumentApp.ParagraphHeading.HEADING2 ||
heading == DocumentApp.ParagraphHeading.HEADING1 ) {
thisBlock.editAsText().replaceText(pattern, '');
}
}
}
/**
* Alias functions for numbering from 1 to N
*/
function numberHeadings12() {
numberHeadings(2);
}
function numberHeadings13() {
numberHeadings(3);
}
function numberHeadings14() {
numberHeadings(4);
}
HOW TO USE IT
1. Go to Extensions > Apps Script menu.
2. Copy/Paste the code from Code.js above.
3. Give it a name
4. Deploy the script (button Deploy > New Deployment)
That's it, now you have a new menu Item in the toolbar of your document.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment