Skip to content

Commit

Permalink
GetNumberFormat #59370
Browse files Browse the repository at this point in the history
  • Loading branch information
askonev committed May 19, 2023
1 parent bd47e11 commit 1d020f2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
51 changes: 51 additions & 0 deletions js/xlsx/smoke/api_range/get_number_format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
builder.CreateFile("xlsx");
var ws = Api.GetActiveSheet();
ws.SetColumnWidth(0, 30);
ws.SetColumnWidth(1, 30);
ws.SetColumnWidth(2, 30);
ws.GetRange("A2").SetNumberFormat("General");
ws.GetRange("A2").SetValue("123456");
ws.GetRange("B2").SetValue("General");
ws.GetRange("A3").SetNumberFormat("0.00");
ws.GetRange("A3").SetValue("123456");
ws.GetRange("B3").SetValue("Number");
ws.GetRange("A4").SetNumberFormat("$#,##0.00");
ws.GetRange("A4").SetValue("123456");
ws.GetRange("B4").SetValue("Currency");
ws.GetRange("A5").SetNumberFormat("_($* #,##0.00_)");
ws.GetRange("A5").SetValue("123456");
ws.GetRange("B5").SetValue("Accounting");
ws.GetRange("A6").SetNumberFormat("m/d/yyyy");
ws.GetRange("A6").SetValue("123456");
ws.GetRange("B6").SetValue("DateShort");
ws.GetRange("A7").SetNumberFormat("[$-F800]dddd, mmmm dd, yyyy");
ws.GetRange("A7").SetValue("123456");
ws.GetRange("B7").SetValue("DateLong");
ws.GetRange("A8").SetNumberFormat("[$-F400]h:mm:ss AM/PM");
ws.GetRange("A8").SetValue("123456");
ws.GetRange("B8").SetValue("Time");
ws.GetRange("A9").SetNumberFormat("0.00%");
ws.GetRange("A9").SetValue("123456");
ws.GetRange("B9").SetValue("Percentage");
ws.GetRange("A10").SetNumberFormat("0%");
ws.GetRange("A10").SetValue("123456");
ws.GetRange("B10").SetValue("Percent");
ws.GetRange("A11").SetNumberFormat("# ?/?");
ws.GetRange("A11").SetValue("123456");
ws.GetRange("B11").SetValue("Fraction");
ws.GetRange("A12").SetNumberFormat("0.00E+00");
ws.GetRange("A12").SetValue("123456");
ws.GetRange("B12").SetValue("Scientific");
ws.GetRange("A13").SetNumberFormat("@");
ws.GetRange("A13").SetValue("123456");
ws.GetRange("B13").SetValue("Text");
for (let i = 2; i <= 13; i++) {
const [A,C] = [1,3];
let range = ws.GetCells(i, A);
ws.GetCells(i, C)
.SetValue('123456')
ws.GetCells(i, C)
.SetNumberFormat(range.GetNumberFormat())
}
builder.SaveFile("xlsx", "GetNumberFormat");
builder.CloseFile();
1 change: 1 addition & 0 deletions lib/doc_builder_testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
require_relative 'doc_builder_testing/doc_builder_wrapper'
require_relative 'doc_builder_testing/exceptions'
require_relative 'doc_builder_testing/web_doc_builder_wrapper'
require_relative '../spec/test_data'
9 changes: 9 additions & 0 deletions spec/test_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module TestData
def self.number_formats
['General', '0.00', '$#,##0.00', '_($* #,##0.00_)', 'm/d/yyyy',
'[$-F800]dddd, mmmm dd, yyyy', '[$-F400]h:mm:ss AM/PM',
'0.00%', '0%', '# ?/?', '0.00E+00'].freeze
end
end
20 changes: 15 additions & 5 deletions spec/xlsx/smoke/api_range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

require 'spec_helper'
describe 'ApiRange section tests' do
before do
include TestData
end

it 'ApiRange | GetCol method' do
xlsx = builder.build_and_parse('js/xlsx/smoke/api_range/get_col.js')
expect(xlsx.worksheets.first.rows[1].cells.first.text).to eq('3')
Expand Down Expand Up @@ -78,17 +82,23 @@
end

it 'ApiRange | NumberFormat method' do
formats = ['General', '0.00', '$#,##0.00', '_($* #,##0.00_)', 'm/d/yyyy',
'[$-F800]dddd, mmmm dd, yyyy', '[$-F400]h:mm:ss AM/PM',
'0.00%', '0%', '# ?/?', '0.00E+00']
xlsx = builder.build_and_parse('js/xlsx/smoke/api_range/setter_number_format.js')
formats.each_with_index do |current_format, i|
TestData.number_formats.each_with_index do |format, i|
expect(xlsx.worksheets.first.rows[i + 1].cells[0].raw_text).to eq('123456')
expect(xlsx.worksheets.first.rows[i + 1].cells[0].style.numerical_format).to eq(current_format)
expect(xlsx.worksheets.first.rows[i + 1].cells[0].style.numerical_format).to eq(format)
end
expect(xlsx.worksheets.first.rows.last.cells[0].style.apply_number_format).to be_truthy
end

it 'ApiRange | GetNumberFormat' do
xlsx = builder.build_and_parse('js/xlsx/smoke/api_range/get_number_format.js')
TestData.number_formats.each_with_index do |format, i|
expect(xlsx.worksheets.first.rows[i + 1].cells[2].raw_text).to eq('123456')
expect(xlsx.worksheets.first.rows[i + 1].cells[2].style.numerical_format).to eq(format)
end
expect(xlsx.worksheets.first.rows.last.cells[2].style.apply_number_format).to be_truthy
end

it 'ApiRange | SetBorders method' do
xlsx = builder.build_and_parse('js/xlsx/smoke/api_range/set_borders.js')
expect(xlsx.worksheets.first.rows[1].cells[0].style.borders.bottom.color).to eq(OoxmlParser::Color.new(49, 133, 154))
Expand Down

0 comments on commit 1d020f2

Please sign in to comment.