Vikas Nale
Vikas Nale

Reputation: 313

How to Add a table in Presentation using ONLYOFFICE API?

We are trying to add table using the ONLYOFFICE API from the presentation editor plugin. We could easily do that with the document editor but are unable to find a way for a presentation.

We tried inserting a table using attached scripts but it's not working. We tried the same in Document Builder also but faced an error while creating a document.

We tried the following:

   oDocContent.InsertContent([oTable]); 

and

   oDocContent.Push(oTable);   

With the following scripts we tried to insert a table in the presentation editor.

Script 1:

builder.CreateFile("pptx");
var oPresentation = Api.GetPresentation();
var oParagraphCell, oTable, oTableStyle, oCell, oTableRow, oParagraph;
var oSlide = oPresentation.GetSlideByIndex(0);
oSlide.RemoveAllObjects();
var oShape = Api.CreateShape("rect", 300 * 36000, 130 * 36000, oFill, oStroke);
oShape.SetPosition(608400, 1267200);
oDocContent = oShape.GetDocContent();
oParagraph = oDocContent.GetElement(0);
oParagraph.AddText("This is an example of the ppt.");
oParagraph = Api.CreateParagraph();
oDocContent.InsertContent([oParagraph]);
oTableStyle = oDocContent.CreateStyle("CustomTableStyle", "table");
oTableStyle.SetBasedOn(oDocument.GetStyle("Bordered - Accent 5"));
oTable = Api.CreateTable(3, 3);
oTable.SetWidth("percent", 100);
oTableRow = oTable.GetRow(0);
oTableRow.SetHeight("atLeast", 1440);
oCell = oTable.GetRow(0).GetCell(0);
oCell.SetVerticalAlign("top");
oParagraphCell = oCell.GetContent().GetElement(0);
oParagraphCell.AddText("Align top");
oCell = oTable.GetRow(0).GetCell(1);
oCell.SetVerticalAlign("center");
oParagraphCell = oCell.GetContent().GetElement(0);
oParagraphCell.AddText("Align center");
oCell = oTable.GetRow(0).GetCell(2);
oCell.SetVerticalAlign("bottom");
oParagraphCell = oCell.GetContent().GetElement(0);
oParagraphCell.AddText("Align bottom");
oTable.SetStyle(oTableStyle);
oDocContent.InsertContent([oTable]);
oSlide.AddObject(oShape);
builder.SaveFile("pptx", "SampleText.pptx");
builder.CloseFile();

Script 2:

builder.CreateFile("pptx");
var oPresentation = Api.GetPresentation();
var oParagraphCell, oTable, oTableStyle, oCell, oTableRow, oParagraph;
var oSlide = oPresentation.GetSlideByIndex(0);
oSlide.RemoveAllObjects();
oFill = Api.CreateSolidFill(Api.CreateRGBColor(61, 74, 107));
oStroke = Api.CreateStroke(0, Api.CreateNoFill());
var oShape = Api.CreateShape("rect", 300 * 36000, 130 * 36000, oFill, oStroke);
oShape.SetPosition(608400, 1267200);
oDocContent = oShape.GetDocContent();
oParagraph = oDocContent.GetElement(0);
oParagraph.AddText("This is an example of the ppt.");
oTableStyle = oDocContent.CreateStyle("CustomTableStyle", "table");
oTableStyle.SetBasedOn(oDocContent.GetStyle("Bordered - Accent 5"));
oTable = Api.CreateTable(3, 3);
oTable.SetWidth("percent", 100);
oTableRow = oTable.GetRow(0);
oTableRow.SetHeight("atLeast", 1440);
oCell = oTable.GetRow(0).GetCell(0);
oCell.SetVerticalAlign("top");
oParagraphCell = oCell.GetContent().GetElement(0);
oParagraphCell.AddText("Align top");
oCell = oTable.GetRow(0).GetCell(1);
oCell.SetVerticalAlign("center");
oParagraphCell = oCell.GetContent().GetElement(0);
oParagraphCell.AddText("Align center");
oCell = oTable.GetRow(0).GetCell(2);
oCell.SetVerticalAlign("bottom");
oParagraphCell = oCell.GetContent().GetElement(0);
oParagraphCell.AddText("Align bottom");
oTable.SetStyle(oTableStyle);
oDocContent.Push(oTable);
oSlide.AddObject(oShape);
builder.SaveFile("pptx", "SampleText.pptx");
builder.CloseFile();

How to add a table in Presentation using ONLYOFFICE API?

Upvotes: 1

Views: 216

Answers (1)

ibnpetr
ibnpetr

Reputation: 527

We could easily do that with the document editor but are unable to find a way for a presentation.

Soon you also will be able to add the tables in ONLYOFFICE Presentations. It is planned in the next versions. Keep in touch with ONLYOFFICE support team, they will let you know.

Upvotes: 1

Related Questions