From 6ae73279693cfad4af692bf3dde3bb1994ec355e Mon Sep 17 00:00:00 2001 From: Yar04ek Date: Tue, 19 Dec 2023 10:41:07 +0100 Subject: [PATCH] ref: ApiTableRowPr.ToJSON --- js/docx/smoke/api_table_row_pr/to_json.js | 23 +++++++++++-------- .../smoke/api_table_row_properties_spec.rb | 14 ++++------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/js/docx/smoke/api_table_row_pr/to_json.js b/js/docx/smoke/api_table_row_pr/to_json.js index 3ec85cbee..c2a77864e 100644 --- a/js/docx/smoke/api_table_row_pr/to_json.js +++ b/js/docx/smoke/api_table_row_pr/to_json.js @@ -5,9 +5,8 @@ builder.CreateFile("docx"); oTableStyle.SetName("My Custom Table Style"); let oTableRowPr = oTableStyle.GetTableRowPr(); oTableRowPr.SetHeight("atLeast", 720); - let jsonTableStyle = oTableStyle.ToJSON(); - GlobalVariable["JSON_TableStyle"] = jsonTableStyle; - oParagraph.AddText(jsonTableStyle); + let jsonTableRowPr = oTableRowPr.ToJSON(); + GlobalVariable["JSON_TableRowPr"] = jsonTableRowPr; builder.CloseFile(); ////////////// @@ -15,13 +14,17 @@ builder.CloseFile(); builder.CreateFile("docx"); let oDocument = Api.GetDocument(); let oParagraph = oDocument.GetElement(0); - let jsonTableStyle = GlobalVariable["JSON_TableStyle"]; - let oTableStyle = Api.FromJSON(jsonTableStyle); + let jsonTableRowPr = GlobalVariable["JSON_TableRowPr"]; + let savedTableRowPr = JSON.parse(jsonTableRowPr); + let oTableStyle = oDocument.CreateStyle("CustomTableStyle", "table"); + oTableStyle.SetName("My Custom Table Style"); let oTable = Api.CreateTable(3, 3); oTable.SetStyle(oTableStyle); - let jsonTable = oTableStyle.ToJSON(); - let oParagraph2 = Api.CreateParagraph(); - oParagraph2.AddText(jsonTable); - oDocument.Push(oParagraph2); -builder.SaveFile("docx", "TableStyleToJSON.docx"); + for (let i = 0; i < oTable.GetRowsCount(); i++) { + let oRow = oTable.GetRow(i); + oRow.SetHeight(savedTableRowPr.trHeight.hRule, savedTableRowPr.trHeight.val); + } + let jsonTable = oTable.ToJSON(); + oParagraph.AddText(jsonTable); +builder.SaveFile("docx", "CustomTableWithJSON.docx"); builder.CloseFile(); diff --git a/spec/docx/smoke/api_table_row_properties_spec.rb b/spec/docx/smoke/api_table_row_properties_spec.rb index 8851deace..97b93db93 100644 --- a/spec/docx/smoke/api_table_row_properties_spec.rb +++ b/spec/docx/smoke/api_table_row_properties_spec.rb @@ -19,14 +19,10 @@ it 'ApiTableRowPr | ToJson method' do docx = builder.build_and_parse('js/docx/smoke/api_table_row_pr/to_json.js') - json = docx.elements[1].nonempty_runs.map(&:text).join - parsed_json = JSON.parse(json) - expect(parsed_json).to include('name' => 'My Custom Table Style') - expect(parsed_json['tblPr']).to include('tblOverlap' => 'never', 'inline' => false, 'type' => 'tablePr') - expect(parsed_json['trPr']).to include('type' => 'tableRowPr') - expect(parsed_json['trPr']['trHeight']).to include('val' => 720, 'hRule' => 'atLeast') - expect(parsed_json['trPr']['trHeight']['val']).to eq(720) - expect(parsed_json['trPr']['trHeight']['hRule']).to eq('atLeast') - expect(parsed_json['styleType']).to eq('tableStyle') + full_json = JSON.parse(docx.elements[0].nonempty_runs.map(&:text).join) + expected_json_fragment = {"trHeight" => {"val" => 720, "hRule" => "atLeast"}, "type" => "tableRowPr"} + table_rows = full_json['content'].select { |content_item| content_item['type'] == 'tblRow' } + table_rows.each { |row| expect(row['trPr']).to eq(expected_json_fragment) } + expect(table_rows.size).to eq(3) end end