Skip to content

Commit

Permalink
[ref] DocContent to json
Browse files Browse the repository at this point in the history
  • Loading branch information
askonev committed Feb 21, 2023
1 parent 1d1ffb6 commit 7c1f0c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
22 changes: 19 additions & 3 deletions js/docx/smoke/api_document_content/to_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@ builder.CreateFile("docx");
var oDrawing = Api.CreateShape("rect", 3212465, 963295, oFill, oStroke);
oParagraph.AddDrawing(oDrawing);
var oDocContent = oDrawing.GetDocContent();
// Add Hyperlink
oParagraph = oDocContent.GetElement(0);
oParagraph.AddText("Simple text");
oParagraph.AddText('Hyperlink');
var oRange = oParagraph.GetRange(0, 8);
oRange.AddHyperlink('https://api.onlyoffice.com')
// Add BlockLvlSdt
var oBlockLvlSdt = Api.CreateBlockLvlSdt();
oBlockLvlSdt.AddText('oBlockLvlSdt');
oDocContent.Push(oBlockLvlSdt);
// Add Table
var oTableStyle = oDocument.CreateStyle("CustomTableStyle", "table");
oTableStyle.SetBasedOn(oDocument.GetStyle("Bordered - Accent 5"));
var oTable = Api.CreateTable(3, 3);
oTable.SetWidth("percent", 100);
oTable.SetStyle(oTableStyle);
oDocContent.Push(oTable);
var json = oDocContent.ToJSON(true, true);
GlobalVariable["JSON"] = json;
builder.CloseFile();
Expand All @@ -17,10 +31,12 @@ builder.CloseFile();
builder.CreateFile("docx");
var json = GlobalVariable["JSON"]
var oDocContentFromJSON = Api.FromJSON(json);
Api.ReplaceDocumentContent(oDocContentFromJSON);
var oDocument = Api.GetDocument();
for( let el = 0; el < oDocContentFromJSON.GetElementsCount(); el++ ) {
oDocument.Push(oDocContentFromJSON.GetElement(el))
}
var oParagraph = Api.CreateParagraph();
oParagraph.AddText(json);
oDocument.Push(oParagraph);
builder.SaveFile("docx", "ParagraphToJSON.docx");
builder.SaveFile("docx", "DocContentToJSON.docx");
builder.CloseFile();
9 changes: 7 additions & 2 deletions spec/docx/smoke/api_document_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@

it 'ApiDocumentContent | ToJSON method' do
docx = builder.build_and_parse('js/docx/smoke/api_document_content/to_json.js')
expect(docx.elements[0].nonempty_runs.first.text).to eq('Simple text')
json = JSON.parse(docx.elements[1].nonempty_runs.first.text)
expect(docx.elements[1].hyperlink.action.to_s).to eq('external_link')
# TODO: add block content control checking
expect(docx.elements[4].number).to eq(3)
json = JSON.parse(docx.elements[7].nonempty_runs.first.text)
expect(json['type']).to eq('docContent')
expect(json['content'][0]['content'][2]['type']).to eq('hyperlink')
expect(json['content'][1]['type']).to eq('blockLvlSdt')
expect(json['content'][2]['type']).to eq('table')
end
end

0 comments on commit 7c1f0c4

Please sign in to comment.