admin管理员组

文章数量:1414614

I am tired of using Microsft Word.

I really like Google Docs. But I need to automate some things.

What do I want

I want to create a simple shortcut that automates some styles.

  1. on a document I will type a text.
  2. I select the Text
  3. I press a custom shortcut
  4. The text is set to blue and bold.

Why am I asking

There is a lot of good tutorials and documentation for automating Google Spreadsheet. I tried to find how to record a macro on Google Docs, and many variations. I just can not find a way to do it.

I have searched Stack Overflow, and again I cannot find.

This kind of automation, I think it is easy, but will save a lot of my time when keeping notes.

I am tired of using Microsft Word.

I really like Google Docs. But I need to automate some things.

What do I want

I want to create a simple shortcut that automates some styles.

  1. on a document I will type a text.
  2. I select the Text
  3. I press a custom shortcut
  4. The text is set to blue and bold.

Why am I asking

There is a lot of good tutorials and documentation for automating Google Spreadsheet. I tried to find how to record a macro on Google Docs, and many variations. I just can not find a way to do it.

I have searched Stack Overflow, and again I cannot find.

This kind of automation, I think it is easy, but will save a lot of my time when keeping notes.

Share Improve this question edited Jun 13, 2020 at 19:15 0Valt 10.4k9 gold badges39 silver badges64 bronze badges asked Jun 13, 2020 at 18:56 Andre NevaresAndre Nevares 7417 silver badges26 bronze badges 1
  • 3 By "shortcut", are you referring to keyboard shortcuts? Have you already read developers.google./docs and developers.google./apps-script/guides/docs? – Wicket Commented Jun 13, 2020 at 19:31
Add a ment  | 

1 Answer 1

Reset to default 3
function setStyleBoldAndBlue() {
  const doc=DocumentApp.getActiveDocument();
  var BoldBlue={};
  BoldBlue[DocumentApp.Attribute.BOLD]=true;
  BoldBlue[DocumentApp.Attribute.FOREGROUND_COLOR]='#3c69f2';
  let selection=doc.getSelection();
  if(selection) {
    var selectedElements = selection.getRangeElements();
    for(var i=0;i<selectedElements.length;i++) {
      var selElem = selectedElements[i];
      var el = selElem.getElement();
      var isPartial = selElem.isPartial();
      if(isPartial) {
        var selStart = selElem.getStartOffset();
        var selEnd = selElem.getEndOffsetInclusive();
        el.asText().setAttributes(selStart, selEnd, BoldBlue);
      }else {
        var selStart = selElem.getStartOffset();
        var selEnd = selElem.getEndOffsetInclusive();
        el.asParagraph().setAttributes(BoldBlue)
      }
    }
  }
}

function menu() {
  DocumentApp.getUi().createMenu('MyMenu')
  .addItem('Bold and Blue', 'setStyleBoldAndBlue')
  .addToUi();  
}

function onOpen() {
  menu();
}

You can access through the MyMenu or attach it to a button. It's not a macro. It's a script.

本文标签: javascriptHow to create a macro in Google DocsStack Overflow