【问题标题】:How to format table cell in OnlyOffice Document Editor using Javascript API如何使用 Javascript API 在 OnlyOffice 文档编辑器中格式化表格单元格
【发布时间】:2017-12-08 11:43:56
【问题描述】:

我正在开发 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");';
            }
        }

请指导..

【问题讨论】:

    标签: javascript onlyoffice


    【解决方案1】:

    我从选项 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2012-10-05
      相关资源
      最近更新 更多