Skip to content

Commit

Permalink
[8.0.0][CDE] Document.ToJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
askonev committed Mar 25, 2024
1 parent 2b6abc3 commit 93a7b5f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
61 changes: 61 additions & 0 deletions js/docx/smoke/api_document/to_json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
builder.CreateFile("docx");
// oParagraph
var oDocument = Api.GetDocument();
var oParagraph = Api.CreateParagraph();
oParagraph.AddText("paragraph");
oDocument.Push(oParagraph);

// oTable
var oTableStyle = oDocument.CreateStyle("CustomTableStyle", "table");
oTableStyle.SetBasedOn(oDocument.GetStyle("Bordered"));
var oTable = Api.CreateTable(3, 3);
oTable.SetWidth("percent", 100);
oTable.SetTableDescription("Empty table");
oTable.SetStyle(oTableStyle);
var oCell = oTable.GetCell(0, 0);
oCell.GetContent().GetElement(0).AddText(oTable.GetClassType());
oDocument.Push(oTable);

// oBlockLvlSdt
var oBlockLvlSdt = Api.CreateBlockLvlSdt();
oBlockLvlSdt.GetContent().GetElement(0).AddText(oBlockLvlSdt.GetClassType());
oParagraph = Api.CreateParagraph();
oParagraph.AddText("This is a block text content control.");
oBlockLvlSdt.Push(oParagraph);
oDocument.Push(oBlockLvlSdt)

var bWriteDefaultTextPr = false
// Specifies if the default text properties will be written to the JSON object or not.
var bWriteDefaultParaPr = false
// Specifies if the default paragraph properties will be written to the JSON object or not.
var bWriteTheme = false
// Specifies if the document theme will be written to the JSON object or not.
var bWriteSectionPr = false
// Specifies if the document section properties will be written to the JSON object or not.
var bWriteNumberings = false
// Specifies if the document numberings will be written to the JSON object or not.
var bWriteStyles = false
// Specifies if the document styles will be written to the JSON object or not.

var json = oDocument.ToJSON(bWriteDefaultTextPr,
bWriteDefaultParaPr,
bWriteTheme,
bWriteSectionPr,
bWriteNumberings,
bWriteStyles);

GlobalVariable["JSON"] = json;
builder.CloseFile();

////////////////////////

builder.CreateFile("docx");
var json = GlobalVariable["JSON"];
var oDocumentFromJSON = Api.FromJSON(json);
var oDocument = Api.GetDocument();
oDocument.GetElement(0).AddText(json);
oDocument.Push(oDocumentFromJSON[1]);
oDocument.Push(oDocumentFromJSON[2]);
oDocument.Push(oDocumentFromJSON[4]);
builder.SaveFile("docx", "DocumentToJSON.docx");
builder.CloseFile();
9 changes: 9 additions & 0 deletions spec/docx/smoke/api_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,13 @@
expect(docx.elements[0].sector_properties.notes[3].type).to eq('footer2')
expect(docx.elements[0].sector_properties.notes[3].elements.first.nonempty_runs.first.text).to eq('This is an even page footer')
end

it 'ApiDocument | ToJSON' do
docx = builder.build_and_parse('js/docx/smoke/api_document/to_json.js')
json = JSON.parse(docx.elements[0].nonempty_runs[0].text)
expect(json['type']).to eq('document')
expect(json['content'][1]['type']).to eq(docx.elements[1].nonempty_runs[0].text)
expect(json['content'][2]['type']).to eq(docx.elements[2].rows[0].cells[0].elements[0].nonempty_runs[0].text)
expect(json['content'][4]['type']).to eq(docx.elements[4].sdt_content.elements.first.nonempty_runs[0].text)
end
end

0 comments on commit 93a7b5f

Please sign in to comment.