Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Add Text at Cursor and Set Headings using Google Apps Script in Docs

Trying to insert specific text at my cursor to a Google Doc via a custom menu created via Google Apps Script. I am then trying to format that text to specific headings, body, etc. The below code is not working and is inserting all text with the same format as the surrounding text within the Google Doc. I have tried a number of different methods to accomplish this but am hoping for some help or tips!

Custom Google Doc Menu

Expected Result

Actual Result

function menuItem12() {
  var cursor = DocumentApp.getActiveDocument().getCursor();
 
  var headerAttr = {}
  headerAttr[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING2;

  var bodyAttr = {}
  bodyAttr[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.NORMAL;
  
  var text = cursor.insertText("Resource Link");
  text.setLinkUrl("https://www.google.com");

  var header = cursor.insertText("Resource Heading\n");
  header.editAsText().setAttributes(0, header.getText().length, headerAttr);


  DocumentApp.getUi().alert('Resources added.');
  
  }

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!