How to modify tables/create tables of a custom size in Presentation

Hello,
So here is my issue: I’m trying to automate some document generation by modifying a template odp file. In said document, I have to insert some data in a table and make it fit the rest of my slide.

The problem is that I cannot find any way to either select a given table in the template in order to change its content, or to create a new table with the given dimensions(including individual column width). After reading the documentation, I couldn’t find any information on either subject, so I was wondering if it is possible at all?

Specifically, ONLYOFFICE Api Documentation - CreateTable (which does take parameters) does not specify table dimensions, nor is there any relevant method in ONLYOFFICE Api Documentation - ApiTable

DocumentBuilder version: 7.5

hey @Quentin :handshake:

Hi @Quentin, I’ve been having a similar problem lately. But I solved it by modify the source codes of document builder sdkjs.

slide/apiBuilder.js : line 709

Api.prototype.CreateTable = function(nCols, nRows, width, height, grid){
    var oPresentation = private_GetPresentation();
    var oSlide = private_GetCurrentSlide();
    if(oPresentation && oSlide){
        var oGraphicFrame = oPresentation.Create_TableGraphicFrame(nCols, nRows, oSlide, oPresentation.DefaultTableStyleId, width, height, grid);
        var content = oGraphicFrame.graphicObject.Content, i;
        if (!AscFormat.isRealNumber(height)) {
            for(i = 0; i < content.length; ++i)
            {
                content[i].Set_Height(0, Asc.linerule_AtLeast );
            }
        }
        return new ApiTable(oGraphicFrame);
    }
    return null;
};

slide/Editor/Format/Presentation.js : line 6088

CPresentation.prototype.Create_TableGraphicFrame = function (Cols, Rows, Parent, StyleId, Width, Height, DefaultGrid, PosX, PosY, bInline) {
	var W;
	if (AscFormat.isRealNumber(Width)) {
		W = Width;
	} else {
		W = this.GetWidthMM() * 2 / 3;
	}
	var X, Y;
	if (AscFormat.isRealNumber(PosX) && AscFormat.isRealNumber(PosY)) {
		X = PosX;
		Y = PosY;
	} else {
		X = (this.GetWidthMM() - W) / 2;
		Y = this.GetHeightMM() / 5;
	}
	var Inline = false;
	if (AscFormat.isRealBool(bInline)) {
		Inline = bInline;
	}
	var Grid = [];
	
	if (Array.isArray(DefaultGrid) && Cols === DefaultGrid.length) {
		Grid = DefaultGrid
	} else {
		for (var Index = 0; Index < Cols; Index++)
			Grid[Index] = W / Cols;
	}
	...

After above modification, you can create table by document builder as follows:

var oPresentation = Api.GetPresentation();
var oSlide = oPresentation.GetCurrentSlide();
var slideWidth = oSlide.GetWidth() / 36000
var tableWidth = slideWidth - 20
var tableHeight = slideWidth / 3
var oTable = Api.CreateTable(2, 4, tableWidth, tableHeight, [tableWidth / 4, tableWidth * 3 / 4]);

The effect is shown below:

1 Like