Skip to content

Commit

Permalink
[feature] GetAllComments() tests (#1163)
Browse files Browse the repository at this point in the history
* GetAllComments method tests

* GetAllComments method tests
  • Loading branch information
lemonowl authored Jul 18, 2024
1 parent 4cd95b1 commit 92d3176
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
48 changes: 48 additions & 0 deletions js/docx/smoke/api_document/get_all_comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
builder.CreateFile("docx");
var oDocument = Api.GetDocument();
oDocument.AddComment("This is a comment.", "John", "uid-1");
Api.AddComment(oDocument, "This is another comment.", "Jane", "uid-2");

var oParagraph = oDocument.GetElement(0);
oParagraph.AddText("This is just a sample paragraph.");
oParagraph.AddComment("Comment for paragraph", "Jane", "uid-2");
oParagraph.AddLineBreak();

var oRun = Api.CreateRun();
oRun.AddText("This is just a sample run. Nothing special.");
oParagraph.AddElement(oRun);
oRun.AddComment("Comment for run", "James", "uid-3");

var oRange = oRun.GetRange(28, 44);
oRange.AddComment("Comment for range", "Julia", "uid-4");

var oTableStyle = oDocument.CreateStyle("CustomTableStyle", "table");
oTableStyle.SetBasedOn(oDocument.GetStyle("Bordered"));
var oTable = Api.CreateTable(3, 3);
oTable.SetWidth("percent", 100);
oTable.SetStyle(oTableStyle);
oDocument.Push(oTable);
oTable.AddComment("Comment for table", "John", "uid-1");

var oBlockLvlSdt = Api.CreateBlockLvlSdt();
oBlockLvlSdt.GetContent().GetElement(0).AddText("This is a block text content control.");
oDocument.Push(oBlockLvlSdt);
oBlockLvlSdt.AddComment("Comment for block content control", "James", "uid-3");

oParagraph = Api.CreateParagraph();
var oInlineLvlSdt = Api.CreateInlineLvlSdt();
oInlineLvlSdt.AddText("This is an inline text content control.");
oParagraph.AddInlineLvlSdt(oInlineLvlSdt);
oDocument.Push(oParagraph);
oInlineLvlSdt.AddComment("Comment for inline content control", "Julia", "uid-4");

var aComments = oDocument.GetAllComments();
var commentsArray = [];
for (var i=0; i<aComments.length; i++) {
commentsArray.push(aComments[i].GetText());
}
oParagraph = Api.CreateParagraph();
oParagraph.AddText(commentsArray.join(", "));
oDocument.Push(oParagraph);
builder.SaveFile("docx", "GetAllComments.docx");
builder.CloseFile();
21 changes: 21 additions & 0 deletions js/pptx/smoke/api_presentation/get_all_comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
builder.CreateFile("pptx");
var oPresentation = Api.GetPresentation();
Api.pluginMethod_AddComment({"UserName": "John Smith", "Text": "This is a comment."});
Api.pluginMethod_AddComment({"UserName": "Mark Potato", "Text": "This is another comment."});

var oSlide = oPresentation.GetSlideByIndex(0);
oSlide.RemoveAllObjects();
var oShape = Api.CreateShape("rect", 300 * 36000, 130 * 36000);
var oDocContent = oShape.GetDocContent();
var oParagraph = oDocContent.GetElement(0);

var arrComments = oPresentation.GetAllComments();
var commentsArray = [];
for (var i=0; i<arrComments.length; i++) {
commentsArray.push(arrComments[i].GetText());
}
oParagraph.AddText(commentsArray.join(", "));
oParagraph.SetHighlight("lightGray");
oSlide.AddObject(oShape);
builder.SaveFile("pptx", "GetAllComments.pptx");
builder.CloseFile();
14 changes: 14 additions & 0 deletions js/xlsx/smoke/api/get_all_comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
builder.CreateFile("xlsx");
Api.AddComment("This is a comment.", "John Smith", "uid-1");
Api.AddComment("This is another comment.", "Mark Potato", "uid-2");
var oWorksheet = Api.GetActiveSheet();
var oRange = oWorksheet.GetRange("D1");
oRange.AddComment("Comment for range", "James", "uid-3");

var arrComments = Api.GetAllComments();
var commentsArray = [];
for (var i=0; i<arrComments.length; i++) {
oWorksheet.GetRange("A" + (i+1)).SetValue(arrComments[i].GetAuthorName() + ":" + arrComments[i].GetText());
}
builder.SaveFile("xlsx", "GetAllComments.xlsx");
builder.CloseFile();
13 changes: 12 additions & 1 deletion spec/docx/smoke/api_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,23 @@
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
it 'ApiDocument | ToJSON method' 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

it 'ApiDocument | GetAllComments method' do
docx = builder.build_and_parse('js/docx/smoke/api_document/get_all_comments.js')
comments_text = docx.comments_document.comments_array.reverse.map do |item|
item.paragraphs.first.nonempty_runs.first.text
end
docx.comments.comments_array.reverse_each do |item|
comments_text.append(item.paragraphs.first.nonempty_runs.first.text)
end
expect(comments_text.join(', ')).to eq(docx.elements.last.nonempty_runs.first.text)
end
end
7 changes: 7 additions & 0 deletions spec/pptx/smoke/api_presentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@
expect(pptx.slide_size.height).to eq(OoxmlParser::OoxmlSize.new(190 * 36_000, :emu))
expect(pptx.slide_size.width).to eq(OoxmlParser::OoxmlSize.new(254 * 36_000, :emu))
end

it 'ApiPresentation | GetAllComments method' do
pptx = builder.build_and_parse('js/pptx/smoke/api_presentation/get_all_comments.js')
comments_text = pptx.comments.list.map(&:text)
slide_data = pptx.slides[0].common_slide_data.shape_tree
expect(comments_text.join(', ')).to eq(slide_data.elements[0].text_body.paragraphs[0].runs[0].text)
end
end
8 changes: 8 additions & 0 deletions spec/xlsx/smoke/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,12 @@
xlsx = builder.build_and_parse('js/xlsx/smoke/api/format_property.js')
expect(xlsx.worksheets[0].rows[0].cells.first.text).to eq('|05:04:23 PM|')
end

it 'Api | GetAllComments method' do
xlsx = builder.build_and_parse('js/xlsx/smoke/api/get_all_comments.js')
expect(xlsx.worksheets.first.rows_raw.size).to eq(3)
expect(xlsx.worksheets.first.rows_raw[0].cells_raw.first.raw_text).to eq('John Smith:This is a comment.')
expect(xlsx.worksheets.first.rows_raw[1].cells_raw.first.raw_text).to eq('Mark Potato:This is another comment.')
expect(xlsx.worksheets.first.rows_raw[2].cells_raw.first.raw_text).to eq('James:Comment for range')
end
end

0 comments on commit 92d3176

Please sign in to comment.