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

时间:2017-12-08 11:43:57

标签: javascript onlyoffice

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

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

我尝试过以下方法为细胞添加粗体设置,没有运气......

选项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 个答案:

答案 0 :(得分:1)

我从选项1中获取了代码并制作了插件。 你错过了分号

  

sScript + =&#39; oParagraph = oCell.GetContent()。GetElement(0)&#39;;

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