如何使用 Javascript API 在 OnlyOffice 文档编辑器中格式化 table 单元格

How to format table cell in OnlyOffice Document Editor using Javascript API

我正在处理 OnlyOffice 服务器集成以使用自定义插件,该插件将用于在文档、电子表格和演示文稿中生成图表和 tables。

添加 table 时,我无法将格式应用于特定单元格,例如粗体和颜色。

我尝试了以下方法来为单元格添加粗体设置,但没有成功...

选项 1

  sScript += 'var oTable,oTableStyle, oCell, oTableRow, oParagraph, oRun, oTableRowPr;';
        sScript += 'oTableStyle = oDocument.CreateStyle("CustomTableStyle", "table");';
        sScript += 'oTable = Api.CreateTable(4,' + rowCnt + ');';
        sScript += 'oTable.SetWidth("percent", 100);';        

        for (var iRow = 0; iRow < rowCnt ; iRow++) {
           sScript += 'oTableRow = oTable.GetRow(' + iRow + ');';

           for (var iCol = 0; iCol < 4; iCol++) {
                 sScript += 'oCell = oTableRow.GetCell(' + iCol + ');';
                 sScript += 'oParagraph = oCell.GetContent().GetElement(0)';
                 sScript += 'oRun = Api.CreateRun();';
                 sScript += 'oRun.SetBold(true);';
                 sScript += 'oRun.AddText("Test Element");';
                 sScript += 'oParagraph.AddElement(oRun);';
            }
        }

选项 2

sScript += 'var oTable,oTableStyle, oCell, oTableRow, oParagraph, oRun, oTableRowPr;';
        sScript += 'oTableStyle = oDocument.CreateStyle("CustomTableStyle", "table");';
        sScript += 'oTable = Api.CreateTable(4,' + rowCnt + ');';
        sScript += 'oTable.SetWidth("percent", 100);';        

        for (var iRow = 0; iRow < rowCnt ; iRow++) {
            sScript += 'oTableRow = oTable.GetRow(' + iRow + ');';
            for (var iCol = 0; iCol < 4; iCol++) {
                sScript += 'oCell = oTableRow.GetCell(' + iCol + ');';
                sScript += ' oCell.GetContent().GetElement(0).SetBold(true);';
                sScript += 'oCell.GetContent().GetElement(0).AddText("Firm Name");';
            }
        }

请指导..

我从选项 1 中获取了您的代码并制作了插件。 您在行

中漏掉了一个分号

sScript += 'oParagraph = oCell.GetContent().GetElement(0)';

sScript += 'oParagraph = oCell.GetContent().GetElement(0);';

我的插件代码:

(function(window, undefined){

window.Asc.plugin.init = function()
{
    var rowCnt = 7;
    var sScript = 'var oDocument = Api.GetDocument();';
    sScript += 'var oTable,oTableStyle, oCell, oTableRow, oParagraph, oRun, oTableRowPr;';
    sScript += 'oTableStyle = oDocument.CreateStyle("CustomTableStyle", "table");';
    sScript += 'oTable = Api.CreateTable(4,' + rowCnt + ');';
    sScript += 'oTable.SetWidth("percent", 100);';        

    for (var iRow = 0; iRow < rowCnt ; iRow++) {
       sScript += 'oTableRow = oTable.GetRow(' + iRow + ');';

       for (var iCol = 0; iCol < 4; iCol++) {
             sScript += 'oCell = oTableRow.GetCell(' + iCol + ');';
             sScript += 'oParagraph = oCell.GetContent().GetElement(0);';
             sScript += 'oRun = Api.CreateRun();';
             sScript += 'oRun.SetBold(true);';
             sScript += 'oRun.AddText("Test Element");';
             sScript += 'oParagraph.AddElement(oRun);';
        }
    }
    sScript += 'oDocument.InsertContent([oTable]);';
    window.Asc.plugin.info.recalculate = true;
    this.executeCommand("close", sScript);
};

window.Asc.plugin.button = function(id)
{
};
})(window, undefined);

有效。 Result