Create content control list
Creates a ContentControlList from all single-level numbered lists throughout the document, as well as from the manually selected text section.
(function() {
let doc = Api.GetDocument();
let contentControlList; // Reference to the current content control list (dropdown)
let contentControlListIndex = 0; // Tracks the number of content control lists created
// Helper function: Converts all numbered paragraphs in the selected part of the document to content control lists
function processSelectedParagraphs(selectedParagraphs) {
let numberedParagraphsIndex = [];
for (let i = 0; i < selectedParagraphs.length; i++) {
let numbering = selectedParagraphs[i].GetNumbering(); // Used to collect indexes of numbered paragraphs for deletion
if (numbering && numbering.GetClassType() === 'numberingLevel') {
numberedParagraphsIndex.push(i);
let text = selectedParagraphs[i].GetText().trim();
addContentControlList(i, text, selectedParagraphs[i]);
}
}
// Delete the original numbered paragraphs
for (let i = 0; i < numberedParagraphsIndex.length; i++) {
let deleteParagraphIndex = numberedParagraphsIndex[i];
selectedParagraphs[deleteParagraphIndex].Delete();
}
}
// Helper function: Converts all numbered paragraphs in the entire document into content control lists
function processAllDocument() {
let numberedParagraphs = doc.GetAllNumberedParagraphs();
console.log(numberedParagraphs)
for (let i = 0; i < numberedParagraphs.length; i++) {
let text = numberedParagraphs[i].GetText().trim();
addContentControlList(i, text, numberedParagraphs[i]);
}
// Delete the original numbered paragraphs
for (let i = 0; i < numberedParagraphs.length; i++) {
numberedParagraphs[i].Delete();
}
}
// Helper function: Creates a content control list from a numbered paragraph
function addContentControlList(i, text, paragraph) {
// Match formats like "1.", "1)", "A.", "a)", "I." etc.
const starterRegex = /^(1[\.\)]|[aAiI][\.\)]?)\s+/;
if (starterRegex.test(text)) {
// Start a new dropdown
contentControlListIndex++;
let newPara = Api.CreateParagraph();
newPara.AddText(' ');
paragraph.InsertParagraph(newPara, "before", true);
newPara.GetRange(0, 1).Select();
let control = doc.AddDropDownListContentControl([]);
contentControlList = control.GetDropdownList();
contentControlList.Add(text, text, i);
} else {
// Append to the current dropdown
contentControlList.Add(text, text, i);
}
}
// Check if there is a selection in the document and run the appropriate function
let selectedRange = doc.GetRangeBySelect();
let selectedText = selectedRange ? selectedRange.GetText().trim() : "";
if (selectedText.length > 0) {
let selectedParagraphs = selectedRange.GetAllParagraphs();
processSelectedParagraphs(selectedParagraphs);
} else {
processAllDocument();
}
})();
Methods used: GetDocument, GetNumbering, GetClassType, GetText, Delete, GetAllNumberedParagraphs, CreateParagraph, AddText, InsertParagraph, GetRange, Select, AddDropDownListContentControl, GetDropdownList, Add, GetRangeBySelect, GetAllParagraphs
Result