跳到主要内容

插入列表

此函数在当前光标位置或文档的开头/末尾创建简单的编号列表或项目符号列表。

提示词

  • 创建包含以下项目的编号列表:"第一项"、"第二项"、"第三项"
  • 在文档开头插入包含以下项目的项目符号列表:"任务 1"、"任务 2"、"任务 3"

函数注册

let func = new RegisteredFunction({
name: "insertList",
description: "Use this function to create simple numbered or bulleted lists at the current cursor position or at the start/end of the document.",
parameters: {
type: "object",
properties: {
items: { type: "array", description: "array of strings representing list items", items: { type: "string" } },
listType: { type: "string", description: "'numbered' for numbered list, 'bulleted' for bulleted list (default is 'bulleted')" },
position: { type: "string", description: "where to insert the list - 'current', 'start', or 'end' (default is 'current')" }
},
required: ["items"]
}
});

参数

NameTypeExampleDescription
itemsarray["First item", "Second item", "Third item"]表示列表项的字符串数组。
listTypestring"numbered"'numbered' 表示编号列表,'bulleted' 表示项目符号列表(默认为 'bulleted')。
positionstring"current"列表的插入位置:'current'(当前位置)、'start'(开头)或 'end'(末尾,默认为 'current')。

函数执行

func.call = async function(params) {
Asc.scope.items = params.items || ["Item 1", "Item 2", "Item 3"];
Asc.scope.listType = params.listType || "bulleted";
Asc.scope.position = params.position || "current";

await Asc.Editor.callCommand(function() {
let doc = Api.GetDocument();

if (Asc.scope.position === "start") {
doc.MoveCursorToStart();
} else if (Asc.scope.position === "end") {
doc.MoveCursorToEnd();
let newParagraph = Api.CreateParagraph();
doc.InsertContent([newParagraph]);
} else if (Asc.scope.position === "current") {
let newParagraph = Api.CreateParagraph();
doc.InsertContent([newParagraph]);
}

let paragraphs = [];
let numbering;

if (Asc.scope.listType === "numbered") {
numbering = doc.CreateNumbering("numbered");
} else {
numbering = doc.CreateNumbering("bullet");
}

let numLvl = numbering.GetLevel(0);

for (let i = 0; i < Asc.scope.items.length; i++) {
let item = Asc.scope.items[i];
let paragraph = Api.CreateParagraph();
paragraph.AddText(item);
paragraph.SetNumbering(numLvl);
paragraph.SetContextualSpacing(true);
paragraphs.push(paragraph);
}

doc.InsertContent(paragraphs);
});
};

return func;

使用的方法:GetDocument, MoveCursorToStart, MoveCursorToEnd, CreateParagraph, InsertContent, CreateNumbering, GetLevel, AddText, SetNumbering, SetContextualSpacing, Asc.scope object

结果