diff --git a/js/docx/smoke/api_document/to_json.js b/js/docx/smoke/api_document/to_json.js new file mode 100644 index 000000000..958244a48 --- /dev/null +++ b/js/docx/smoke/api_document/to_json.js @@ -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(); diff --git a/spec/docx/smoke/api_document_spec.rb b/spec/docx/smoke/api_document_spec.rb index 9ecf875f8..0e6d9e916 100644 --- a/spec/docx/smoke/api_document_spec.rb +++ b/spec/docx/smoke/api_document_spec.rb @@ -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