Skip to content

Commit

Permalink
ref: ApiTableRowPr.ToJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Yar04ek committed Dec 19, 2023
1 parent 9cfc3ae commit 6ae7327
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
23 changes: 13 additions & 10 deletions js/docx/smoke/api_table_row_pr/to_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ 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();

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

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();
14 changes: 5 additions & 9 deletions spec/docx/smoke/api_table_row_properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6ae7327

Please sign in to comment.