Google Apps Script: Issues with Preserving Bullet Icons and Resetting Numbering When Inserting List Items into Google Document

I’m working with Google Apps Script to insert/copy text from one Google Document into another. These text can include various types of content, including bullet and numbered lists. However, I’m encountering two issues:

  1. When inserting a bullet list, all bullet icons are not preserved, resulting in a plain list without the intended bullet styling.
  2. When inserting a numbered list, the numbering continues from the last number, rather than restarting from 1. For example, if I insert a text with a list numbered from 1 to 5, and then insert it again, it continues numbering from 6 instead of restarting from 1.

Original document

After Insert

Bellow is my code and I have attached screenshots of original document and when inserting/copying that into new document.
I am expecting that when inserting the clause/text it should preserve as it is with original list formatting which is there in original document shared in screenshots.

How to Use:

  1. Create a new Google Apps Script project and paste the above code.
  2. Save and authorize the script.
  3. Run the script in any Google Document to test it out. The script will add a “Clause Manager” menu from which you can insert clauses by providing a Document ID.

This script should work as an add-on that you can use directly in any Google Document.

function onOpen() {
  DocumentApp.getUi().createMenu('Clause Manager')
    .addItem('Insert Clause', 'insertClause')
    .addToUi();
}

function insertClause() {
  const ui = DocumentApp.getUi();
  const response = ui.prompt('Enter the Clause Document ID:');
  
  if (response.getSelectedButton() == ui.Button.OK) {
    const clauseId = response.getResponseText();
    const document = DocumentApp.getActiveDocument();
    const cursor = document.getCursor();

    if (!cursor) {
      ui.alert('Cannot find a cursor in the document.');
      return;
    }

    const element = cursor.getElement();
    insertClauseInsideDocumentBody(document, element, clauseId, 'ClauseName');
  }
}

function insertClauseInsideDocumentBody(document, element, clauseId, clauseName) {
  try {
    const documentBody = document.getBody();
    let index = documentBody.getChildIndex(element);

    const clauseDocument = DocumentApp.openById(clauseId);
    const clauseBody = clauseDocument.getBody();

    if (clauseBody.getNumChildren() === 0) {
      DocumentApp.getUi().alert('The clause document is empty.');
      return;
    }

    const clauseElements = [];

    for (let i = 0; i < clauseBody.getNumChildren(); i++) {
      const child = clauseBody.getChild(i);
      const childContent = child.copy();
      let clauseElement;

      switch (child.getType()) {
        case DocumentApp.ElementType.PARAGRAPH: {
          const paraElement = documentBody.insertParagraph(index, childContent);
          clauseElement = paraElement;
          index++;
          break;
        }
        case DocumentApp.ElementType.LIST_ITEM: {
          const listElement = documentBody.insertListItem(index, childContent);
          listElement.setAttributes(child.getAttributes());
          clauseElement = listElement;
          index++;
          break;
        }
        case DocumentApp.ElementType.TABLE: {
          const tableElement = documentBody.insertTable(index, childContent);
          clauseElement = tableElement;
          index++;
          break;
        }
        case DocumentApp.ElementType.INLINE_IMAGE: {
          const imageElement = documentBody.insertImage(index, childContent);
          clauseElement = imageElement;
          index++;
          break;
        }
        default:
          clauseElement = null;
          break;
      }

      if (clauseElement) {
        clauseElements.push(clauseElement);
      }
    }

    const rangeBuilder = document.newRange();
    rangeBuilder.addElementsBetween(clauseElements[0].editAsText(), 1, clauseElements[clauseElements.length - 1].editAsText(), clauseElements[clauseElements.length - 1].editAsText().getText().length - 1);
    document.addNamedRange(clauseName, rangeBuilder.build());
    
    DocumentApp.getUi().alert('Clause inserted successfully.');
    
  } catch (error) {
    DocumentApp.getUi().alert('An error occurred: ' + error.message);
  }
}

New contributor

Pankaj Sakhare is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật