跳到主要内容

addTableToSlide

This function adds a table to the specified or the current slide. By default, the table is 194x97 mm and centered.

Prompts

  • Add a 3x3 table on slide 2
  • Add a table with data [["Name", "Age", "City"], ["John", "30", "New York"], ["Jane", "25", "London"]] on the current slide
  • Add a simple 2x4 table

Function registration

let func = new RegisteredFunction();
func.name = "addTableToSlide";
func.description = "Adds a table to the slide (194x97mm, centered)";
func.params = [
"slideNumber (number): slide number to add table to (optional, defaults to current)",
"rows (number): number of rows (optional, defaults to 3)",
"columns (number): number of columns (optional, defaults to 3)",
"data (array): 2D array of cell values - rows x columns (optional)"
];

func.examples = [
"if you need to add a 3x3 table on slide 2, respond with:\n" +
"[functionCalling (addTableToSlide)]: {\"slideNumber\": 2, \"rows\": 3, \"columns\": 3}",
"if you need to add a table with data on current slide, respond with:\n" +
"[functionCalling (addTableToSlide)]: {\"data\": [[\"Name\", \"Age\", \"City\"], [\"John\", \"30\", \"New York\"], [\"Jane\", \"25\", \"London\"]]}",
"if you need to add a simple 2x4 table, respond with:\n" +
"[functionCalling (addTableToSlide)]: {\"rows\": 2, \"columns\": 4}"
];

Parameters

NameTypeExampleDescription
slideNumbernumber2The slide number where the table will be added. If omitted, the table is added to the current slide.
rowsnumber2The number of rows. The default value is 3.
columnsnumber4The number of columns. The default value is 3.
dataarray[["Name", "Age", "City"], ["John", "30", "New York"], ["Jane", "25", "London"]]A two-dimensional array of cell values, structured as rows × columns.

Function execution

func.call = async function (params) {
Asc.scope.params = params;

await Asc.Editor.callCommand(function () {
let presentation = Api.GetPresentation();
let slide;

if (Asc.scope.params.slideNumber) {
slide = presentation.GetSlideByIndex(Asc.scope.params.slideNumber - 1);
}
else {
slide = presentation.GetCurrentSlide();
}

if (!slide) return;

let slideWidth = presentation.GetWidth();
let slideHeight = presentation.GetHeight();

let data = Asc.scope.params.data;
let rows = Asc.scope.params.rows || 3;
let columns = Asc.scope.params.columns || 3;

if (data && Array.isArray(data) && data.length > 0) {
rows = data.length;
if (data[0] && Array.isArray(data[0])) {
columns = data[0].length;
}
}

let tableWidth = 7000000;
let tableHeight = 3500000;
let x = (slideWidth - tableWidth) / 2;
let y = (slideHeight - tableHeight) / 2;

let table = Api.CreateTable(columns, rows);

if (table) {
table.SetPosition(x, y);
table.SetSize(tableWidth, tableHeight);
let rowHeight = tableHeight / rows;
if (data && Array.isArray(data)) {
let rowCount = Math.min(data.length, rows);
for (let rowIdx = 0; rowIdx < rowCount; rowIdx++) {
let row = table.GetRow(rowIdx);
if (Array.isArray(data[rowIdx])) {
let cellCount = Math.min(data[rowIdx].length, columns);
for (let col = 0; col < cellCount; col++) {
let cell = row.GetCell(col);
if (cell) {
let cellContent = cell.GetContent();
if (cellContent) {
cellContent.RemoveAllElements();
let paragraph = Api.CreateParagraph();
let value = data[rowIdx][col];
if (value !== null && value !== undefined) {
paragraph.AddText(value);
cellContent.Push(paragraph);
}
}
}
}
}
}
}

slide.AddObject(table);
}
});
};

return func;

Methods used: GetPresentation, GetCurrentSlide, GetSlideByIndex, GetWidth, GetHeight, CreateTable, SetPosition, SetSize, GetRow, GetCell, GetContent, RemoveAllElements, CreateParagraph, Push, AddText, AddObject, Asc.scope object

Result

addTableToSlide addTableToSlide