autoNumberHeadings icon indicating copy to clipboard operation
autoNumberHeadings copied to clipboard

Numbering schema

Open lpanebr opened this issue 10 years ago • 9 comments

Should allow configuration per level:

  • Number, Alfa upper, Alfa lower, Roman numbering: 4, D, d, IV
  • Character after numbering: tab, ., -, :, custom
  • Character beetwing numbers: ., |, custom

lpanebr avatar May 14 '14 13:05 lpanebr

default starting numbering is 0.0.1 then 0.0.1.1

How can I modify the code to get rid of the extraneous 0.0. before 1 please?

In other words I want 1.1 1.1.1 1.1.1.1 etc not 0.0.1 0.0.1.1 0.0.1.1.1 0.0.1.1.1.1

ke-ss avatar Jan 11 '23 22:01 ke-ss

default starting numbering is 0.0.1 then 0.0.1.1

That's weird. Here's what I get after running the 'Auto Number Headings' menu option with the current code:

image

lpanebr avatar Jan 12 '23 16:01 lpanebr

I identified the problem

The first heading I had used was "Heading 3", I had not used "Headings 1" or "Headings 2"

I had done this because the text size of headings 1 and heading 2 was too big for my document

I almost never use Headings 1 or 2 in google docs

I probably want to save headings 1 and headings 2 so if I need to have big text headings I can use them later, perhaps if I add document sections at a later date

But in the mean time I will just replace heading 3 with heading 1 etc and change the text font size to be smaller which will fix the problem

Changing all of headers 3 to headers 1 is not an insignificant task becasue it is a large document

Probably not a big fix to make, to get header 3 to start at 1.0 in the absence of headers 1 and 2 but I know from my own poor coding skills it would take me forever to make such a modification

thanks for this contribution, it is very helpful, appreciated

thank you

ke-ss avatar Jan 15 '23 16:01 ke-ss

It is both remarkable and surprising that this appears, after an extensive search, to be the only working google docs header numbering script out there.

the script is appreciated

ke-ss avatar Jan 15 '23 17:01 ke-ss

I solved the problem of having hundered's of heading 3's

I used your script and added a script I found that moves the headings up or down and added it to the menu This is not my work

Link above each function links to authors for credit

Hopefully this is of some use to someone

function onOpen() {
  DocumentApp.getUi().createMenu('Headings Tools')
  .addItem('Auto Number Headings', 'numberHeadingsAdd')
  .addItem('Clear Heading Numbers', 'numberHeadingsClear')
  .addItem('Higher level 6>5, 5>4, 3>2, 2>1', 'increaseHeadingLevel')
  .addItem('Lower level 1>2, 2>3, 3>4, 5>6', 'decreaseHeadingLevel')

  .addToUi();
}

// https://github.com/lpanebr/autoNumberHeadings/blob/master/code.gs
function numberHeadingsAdd(){
  numberHeadings(true);
}

function numberHeadingsClear(){
  numberHeadings(false);
}

function numberHeadings(add){
  var document = DocumentApp.getActiveDocument();
  var body = document.getBody();
  var paragraphs = document.getParagraphs();
  var numbers = [0,0,0,0,0,0];
  for (var i in paragraphs) {
    var element = paragraphs[i];
    var text = element.getText()+'';
    var type = element.getHeading()+'';
    
    // exclude everything but headings
    if (!type.match(/HEADING\d/)) {
      continue;
    }
    
    // exclude empty headings (e.g. page breaks generate these)
    if( text.match(/^\s*$/)){
      continue;
    }

    if (add == true) {
      var level = new RegExp(/HEADING(\d)/).exec(type)[1];
      var numbering = '';
      
      numbers[level]++;
      for (var currentLevel = 1; currentLevel <= 6; currentLevel++) {
        if (currentLevel <= level) {
          numbering += numbers[currentLevel] + '.';
        } else {
          numbers[currentLevel] = 0;
        }
      }
      Logger.log(text);
      var newText = numbering + ' ' + text.replace(/^[0-9\.\s]+/, '');
      element.setText(newText);
      Logger.log([newText]);
    } else {
      Logger.log(text);
      element.setText(text.replace(/^[0-9\.\s]+/, ''));
    }
  }

}

// https://webapps.stackexchange.com/questions/71290/convert-all-headings-x-into-headings-x-1-or-heading-x-1-in-google-documents
function decreaseHeadingLevel() {
  var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();  
  for(var i in pars) {
    var p = pars[i], h = p.getHeading(), d = DocumentApp.ParagraphHeading;
    switch(h) {
      case h.TITLE:
        p.setHeading(d.HEADING1);
        break;
      case h.HEADING1:
        p.setHeading(d.HEADING2);
        break;
      case h.HEADING2:
        p.setHeading(d.HEADING3);
        break;
      case h.HEADING3:
        p.setHeading(d.HEADING4);
        break;
      case h.HEADING4:
        p.setHeading(d.HEADING5);
        break;
      case h.HEADING5:
        p.setHeading(d.HEADING6);
        break;
    }
  }
}

// https://webapps.stackexchange.com/questions/71290/convert-all-headings-x-into-headings-x-1-or-heading-x-1-in-google-documents
function increaseHeadingLevel() {
  var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();  
  for(var i in pars) {
    var p = pars[i], h = p.getHeading(), d = DocumentApp.ParagraphHeading;
    switch(h) {
      case h.HEADING1:
        p.setHeading(d.TITLE);
        break;
      case h.HEADING2:
        p.setHeading(d.HEADING1);
        break;
      case h.HEADING3:
        p.setHeading(d.HEADING2);
        break;
      case h.HEADING4:
        p.setHeading(d.HEADING3);
        break;
      case h.HEADING5:
        p.setHeading(d.HEADING4);
        break;
      case h.HEADING6:
        p.setHeading(d.HEADING5);
        break;
    }
  }
}

ke-ss avatar Jan 15 '23 17:01 ke-ss

Thank you for your interest and comments! I believe what you ask would fall unde some other, maybe new issue. Anyway, I've added feature #19 that may help you. You can structure from level 1 and add numbers. Then you use the decrease option to downgrade the styles and the added numbers will be kept.

lpanebr avatar Jun 09 '23 13:06 lpanebr