Skip to content

Commit

Permalink
Fix boolean values so the output matches Excel and works on Numbers
Browse files Browse the repository at this point in the history
Use 1 or 0 instead of 'true' or 'false' in the XML output
  • Loading branch information
jurriaan committed Apr 4, 2014
1 parent c649ee7 commit 5ccab46
Show file tree
Hide file tree
Showing 21 changed files with 66 additions and 46 deletions.
12 changes: 12 additions & 0 deletions lib/axlsx.rb
Expand Up @@ -137,6 +137,18 @@ def self.sanitize(str)
str.delete!(CONTROL_CHARS)
str
end

# If value is boolean return 1 or 0
# else return the value
# @param [Object] value The value to process
# @return [Object]
def self.booleanize(value)
if value == true || value == false
value ? 1 : 0
else
value
end
end

# Instructs the serializer to not try to escape cell value input.
# This will give you a huge speed bonus, but if you content has <, > or other xml character data
Expand Down
3 changes: 3 additions & 0 deletions lib/axlsx/stylesheet/font.rb
Expand Up @@ -140,6 +140,9 @@ def sz=(v) Axlsx::validate_unsigned_int v; @sz=v end
def to_xml_string(str = '')
str << '<font>'
instance_values.each do |k, v|
if v == true || v == false
v = v ? 1 : 0
end
v.is_a?(Color) ? v.to_xml_string(str) : (str << ('<' << k.to_s << ' val="' << v.to_s << '"/>'))
end
str << '</font>'
Expand Down
2 changes: 1 addition & 1 deletion lib/axlsx/util/serialized_attributes.rb
Expand Up @@ -52,7 +52,7 @@ def serialized_tag(tagname, str, additional_attributes = {}, &block)
def serialized_attributes(str = '', additional_attributes = {})
attributes = declared_attributes.merge! additional_attributes
attributes.each do |key, value|
str << "#{Axlsx.camel(key, false)}=\"#{Axlsx.camel(value, false)}\" "
str << "#{Axlsx.camel(key, false)}=\"#{Axlsx.camel(Axlsx.booleanize(value), false)}\" "
end
str
end
Expand Down
4 changes: 3 additions & 1 deletion lib/axlsx/workbook/worksheet/data_validation.rb
Expand Up @@ -216,7 +216,9 @@ def to_xml_string(str = '')
valid_attributes = get_valid_attributes

str << '<dataValidation '
str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' if (valid_attributes.include?(key.to_sym) and not CHILD_ELEMENTS.include?(key.to_sym)) }.join(' ')
str << instance_values.map do |key, value|
'' << key << '="' << Axlsx.booleanize(value).to_s << '"' if (valid_attributes.include?(key.to_sym) && !CHILD_ELEMENTS.include?(key.to_sym))
end.join(' ')
str << '>'
str << ('<formula1>' << self.formula1 << '</formula1>') if @formula1 and valid_attributes.include?(:formula1)
str << ('<formula2>' << self.formula2 << '</formula2>') if @formula2 and valid_attributes.include?(:formula2)
Expand Down
2 changes: 1 addition & 1 deletion test/workbook/tc_defined_name.rb
Expand Up @@ -42,7 +42,7 @@ def test_to_xml_string
@dn.hidden = true
doc = Nokogiri::XML(@dn.to_xml_string)
assert_equal(doc.xpath("//definedName[@name='_xlnm.Print_Titles']").size, 1)
assert_equal(doc.xpath("//definedName[@hidden='true']").size, 1)
assert_equal(doc.xpath("//definedName[@hidden='1']").size, 1)
assert_equal('Sheet1!A1:A1', doc.xpath('//definedName').text)
end

Expand Down
3 changes: 3 additions & 0 deletions test/workbook/tc_workbook_view.rb
Expand Up @@ -40,6 +40,9 @@ def test_to_xml_string
xml = @book_view.to_xml_string
doc = Nokogiri::XML(xml)
@options.each do |key, value|
if value == true || value == false
value = value ? 1 : 0
end
path = "workbookView[@#{Axlsx.camel(key, false)}='#{value}']"
assert_equal(1, doc.xpath(path).size)
end
Expand Down
2 changes: 1 addition & 1 deletion test/workbook/worksheet/auto_filter/tc_filters.rb
Expand Up @@ -44,7 +44,7 @@ def hidden.value; 'b'; end

def test_to_xml_string
doc = Nokogiri::XML(@filters.to_xml_string)
assert_equal(1, doc.xpath('//filters[@blank="true"]').size)
assert_equal(1, doc.xpath('//filters[@blank=1]').size)
end
end

2 changes: 1 addition & 1 deletion test/workbook/worksheet/tc_break.rb
Expand Up @@ -44,6 +44,6 @@ def test_pt

def test_to_xml_string
doc = Nokogiri::XML(@break.to_xml_string)
assert_equal(doc.xpath('//brk[@id="1"][@min="1"][@max="10"][@pt="false"][@man="true"]').size, 1)
assert_equal(doc.xpath('//brk[@id="1"][@min="1"][@max="10"][@pt=0][@man=1]').size, 1)
end
end
4 changes: 2 additions & 2 deletions test/workbook/worksheet/tc_col.rb
Expand Up @@ -61,11 +61,11 @@ def test_phonetic
def test_to_xml_string
@col.width = 100
doc = Nokogiri::XML(@col.to_xml_string)
assert_equal(1, doc.xpath("//col [@bestFit='#{@col.best_fit}']").size)
assert_equal(1, doc.xpath("//col [@bestFit='#{@col.best_fit ? 1 : 0}']").size)
assert_equal(1, doc.xpath("//col [@max=#{@col.max}]").size)
assert_equal(1, doc.xpath("//col [@min=#{@col.min}]").size)
assert_equal(1, doc.xpath("//col [@width=#{@col.width}]").size)
assert_equal(1, doc.xpath("//col [@customWidth='#{@col.custom_width}']").size)
assert_equal(1, doc.xpath("//col [@customWidth='#{@col.custom_width ? 1 : 0}']").size)
end

def test_style
Expand Down
4 changes: 2 additions & 2 deletions test/workbook/worksheet/tc_conditional_formatting.rb
Expand Up @@ -113,8 +113,8 @@ def test_many_options
:percent => false, :rank => 0, :stdDev => 1, :stopIfTrue => true, :timePeriod => :today,
:formula => "0.0"})
doc = Nokogiri::XML.parse(cf.to_xml_string)
assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage='false'][@bottom='false'][@dxfId=0][@equalAverage='false'][@priority=2][@operator='lessThan'][@text=''][@percent='false'][@rank=0][@stdDev=1][@stopIfTrue='true'][@timePeriod='today']").size)
assert doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage='false'][@bottom='false'][@dxfId=0][@equalAverage='false'][@priority=2][@operator='lessThan'][@text=''][@percent='false'][@rank=0][@stdDev=1][@stopIfTrue='true'][@timePeriod='today']//formula=0.0")
assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage=0][@bottom=0][@dxfId=0][@equalAverage=0][@priority=2][@operator='lessThan'][@text=''][@percent=0][@rank=0][@stdDev=1][@stopIfTrue=1][@timePeriod='today']").size)
assert doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage=0][@bottom=0][@dxfId=0][@equalAverage=0][@priority=2][@operator='lessThan'][@text=''][@percent=0][@rank=0][@stdDev=1][@stopIfTrue=1][@timePeriod='today']//formula=0.0")
end

def test_to_xml
Expand Down
2 changes: 1 addition & 1 deletion test/workbook/worksheet/tc_data_bar.rb
Expand Up @@ -38,7 +38,7 @@ def test_showValue

def test_to_xml_string
doc = Nokogiri::XML.parse(@data_bar.to_xml_string)
assert_equal(doc.xpath(".//dataBar[@minLength=10][@maxLength=90][@showValue='true']").size, 1)
assert_equal(doc.xpath(".//dataBar[@minLength=10][@maxLength=90][@showValue=1]").size, 1)
assert_equal(doc.xpath(".//dataBar//cfvo").size, 2)
assert_equal(doc.xpath(".//dataBar//color").size, 1)
end
Expand Down
22 changes: 11 additions & 11 deletions test/workbook/worksheet/tc_data_validation.rb
Expand Up @@ -160,11 +160,11 @@ def test_whole_decimal_data_time_textLength_to_xml
#test attributes
assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
[@promptTitle='Be carful!'][@prompt='Only values between 5 and 10'][@operator='between'][@errorTitle='Wrong input']
[@error='Only values between 5 and 10'][@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@type='whole']
[@error='Only values between 5 and 10'][@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@type='whole']
[@errorStyle='information']").size)
assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
[@promptTitle='Be carful!'][@prompt='Only values between 5 and 10'][@operator='between'][@errorTitle='Wrong input']
[@error='Only values between 5 and 10'][@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true']
[@error='Only values between 5 and 10'][@showErrorMessage=1][@allowBlank=1][@showInputMessage=1]
[@type='whole'][@errorStyle='information']")

#test forumula1
Expand All @@ -189,11 +189,11 @@ def test_list_to_xml
#test attributes
assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
[@promptTitle='Be carful!'][@prompt='Only values from list'][@errorTitle='Wrong input'][@error='Only values from list']
[@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@showDropDown='true'][@type='list']
[@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@showDropDown=1][@type='list']
[@errorStyle='stop']").size)
assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
[@promptTitle='Be carful!'][@prompt='Only values from list'][@errorTitle='Wrong input'][@error='Only values from list']
[@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@showDropDown='true'][@type='list'][@errorStyle='stop']")
[@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@showDropDown=1][@type='list'][@errorStyle='stop']")

#test forumula1
assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1").size)
Expand All @@ -212,11 +212,11 @@ def test_custom_to_xml

#test attributes
assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1'][@promptTitle='Be carful!']
[@prompt='Only values corresponding formula'][@errorTitle='Wrong input'][@error='Only values corresponding formula'][@showErrorMessage='true']
[@allowBlank='true'][@showInputMessage='true'][@type='custom'][@errorStyle='stop']").size)
[@prompt='Only values corresponding formula'][@errorTitle='Wrong input'][@error='Only values corresponding formula'][@showErrorMessage=1]
[@allowBlank=1][@showInputMessage=1][@type='custom'][@errorStyle='stop']").size)
assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1'][@promptTitle='Be carful!']
[@prompt='Only values corresponding formula'][@errorTitle='Wrong input'][@error='Only values corresponding formula']
[@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@type='custom'][@errorStyle='stop']")
[@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@type='custom'][@errorStyle='stop']")

#test forumula1
assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1").size)
Expand All @@ -240,21 +240,21 @@ def test_multiple_datavalidations_to_xml
#test attributes
assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='A1']
[@promptTitle='Be carful!'][@prompt='Only values between 5 and 10'][@operator='between'][@errorTitle='Wrong input']
[@error='Only values between 5 and 10'][@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@type='whole']
[@error='Only values between 5 and 10'][@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@type='whole']
[@errorStyle='information']").size)
assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='A1']
[@promptTitle='Be carful!'][@prompt='Only values between 5 and 10'][@operator='between'][@errorTitle='Wrong input']
[@error='Only values between 5 and 10'][@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true']
[@error='Only values between 5 and 10'][@showErrorMessage=1][@allowBlank=1][@showInputMessage=1]
[@type='whole'][@errorStyle='information']")

#test attributes
assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='B1']
[@promptTitle='Be carful!'][@prompt='Only values from list'][@errorTitle='Wrong input'][@error='Only values from list']
[@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@showDropDown='true'][@type='list']
[@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@showDropDown=1][@type='list']
[@errorStyle='stop']").size)
assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='B1']
[@promptTitle='Be carful!'][@prompt='Only values from list'][@errorTitle='Wrong input'][@error='Only values from list']
[@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@showDropDown='true'][@type='list'][@errorStyle='stop']")
[@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@showDropDown=1][@type='list'][@errorStyle='stop']")
end

def test_empty_attributes
Expand Down
4 changes: 2 additions & 2 deletions test/workbook/worksheet/tc_header_footer.rb
Expand Up @@ -109,7 +109,7 @@ def test_to_xml_all_values
)

doc = Nokogiri::XML.parse(@hf.to_xml_string)
assert_equal(1, doc.xpath(".//headerFooter[@differentFirst='true'][@differentOddEven='true']").size)
assert_equal(1, doc.xpath(".//headerFooter[@differentFirst=1][@differentOddEven=1]").size)

assert_equal(1, doc.xpath(".//headerFooter/oddHeader").size)
assert_equal('oh', doc.xpath(".//headerFooter/oddHeader").text)
Expand All @@ -134,7 +134,7 @@ def test_to_xml_some_values
)

doc = Nokogiri::XML.parse(@hf.to_xml_string)
assert_equal(1, doc.xpath(".//headerFooter[@differentOddEven='false']").size)
assert_equal(1, doc.xpath(".//headerFooter[@differentOddEven=0]").size)
assert_equal(0, doc.xpath(".//headerFooter[@differentFirst]").size)

assert_equal(1, doc.xpath(".//headerFooter/oddHeader").size)
Expand Down
2 changes: 1 addition & 1 deletion test/workbook/worksheet/tc_icon_set.rb
Expand Up @@ -38,7 +38,7 @@ def test_reverse

def test_to_xml_string
doc = Nokogiri::XML.parse(@icon_set.to_xml_string)
assert_equal(doc.xpath(".//iconSet[@iconSet='3TrafficLights1'][@percent='true'][@reverse='false'][@showValue='true']").size, 1)
assert_equal(doc.xpath(".//iconSet[@iconSet='3TrafficLights1'][@percent=1][@reverse=0][@showValue=1]").size, 1)
assert_equal(doc.xpath(".//iconSet//cfvo").size, 3)
end

Expand Down
2 changes: 1 addition & 1 deletion test/workbook/worksheet/tc_print_options.rb
Expand Up @@ -42,7 +42,7 @@ def test_set_some_values
def test_to_xml
@po.set(:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true)
doc = Nokogiri::XML.parse(@po.to_xml_string)
assert_equal(1, doc.xpath(".//printOptions[@gridLines='true'][@headings='true'][@horizontalCentered='true'][@verticalCentered='true']").size)
assert_equal(1, doc.xpath(".//printOptions[@gridLines=1][@headings=1][@horizontalCentered=1][@verticalCentered=1]").size)
end

def test_grid_lines
Expand Down
2 changes: 1 addition & 1 deletion test/workbook/worksheet/tc_row.rb
Expand Up @@ -111,7 +111,7 @@ def test_to_xml_string_with_custom_height
@row.add_cell 1
@row.height = 20
r_s_xml = Nokogiri::XML(@row.to_xml_string(0, ''))
assert_equal(r_s_xml.xpath(".//row[@r=1][@ht=20][@customHeight='true']").size, 1)
assert_equal(r_s_xml.xpath(".//row[@r=1][@ht=20][@customHeight=1]").size, 1)
end

end
2 changes: 1 addition & 1 deletion test/workbook/worksheet/tc_sheet_calc_pr.rb
Expand Up @@ -13,6 +13,6 @@ def test_full_calc_on_load

def test_to_xml_string
doc = Nokogiri::XML(@sheet_calc_pr.to_xml_string)
assert_equal 1, doc.xpath('//sheetCalcPr[@fullCalcOnLoad="false"]').size
assert_equal 1, doc.xpath('//sheetCalcPr[@fullCalcOnLoad=0]').size
end
end
8 changes: 4 additions & 4 deletions test/workbook/worksheet/tc_sheet_format_pr.rb
Expand Up @@ -74,13 +74,13 @@ def test_thick_bottom

def test_to_xml_string
doc = Nokogiri::XML(@sheet_format_pr.to_xml_string)
assert doc.xpath("sheetFormatPr[@thickBottom='true']")
assert doc.xpath("sheetFormatPr[@thickBottom=1]")
assert doc.xpath("sheetFormatPr[@baseColWidth=5]")
assert doc.xpath("sheetFormatPr[@default_col_width=7.2]")
assert doc.xpath("sheetFormatPr[@default_row_height=5.2]")
assert doc.xpath("sheetFormatPr[@custom_height='true']")
assert doc.xpath("sheetFormatPr[@zero_height='false']")
assert doc.xpath("sheetFormatPr[@thick_top='true']")
assert doc.xpath("sheetFormatPr[@custom_height=1]")
assert doc.xpath("sheetFormatPr[@zero_height=0]")
assert doc.xpath("sheetFormatPr[@thick_top=1]")
assert doc.xpath("sheetFormatPr[@outline_level_row=0]")
assert doc.xpath("sheetFormatPr[@outline_level_col=0]")
end
Expand Down
10 changes: 5 additions & 5 deletions test/workbook/worksheet/tc_sheet_protection.rb
Expand Up @@ -2,9 +2,9 @@
require 'tc_helper.rb'

# <xsd:complexType name="CT_SheetProtection">
# <xsd:attribute name="sheet" type="xsd:boolean" use="optional" default="false"/>
# <xsd:attribute name="objects" type="xsd:boolean" use="optional" default="false"/>
# <xsd:attribute name="scenarios" type="xsd:boolean" use="optional" default="false"/>
# <xsd:attribute name="sheet" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="objects" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="scenarios" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="formatCells" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="formatColumns" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="formatRows" type="xsd:boolean" use="optional" default="true"/>
Expand All @@ -13,11 +13,11 @@
# <xsd:attribute name="insertHyperlinks" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="deleteColumns" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="deleteRows" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="selectLockedCells" type="xsd:boolean" use="optional" default="false"/>
# <xsd:attribute name="selectLockedCells" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="sort" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="autoFilter" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="pivotTables" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="selectUnlockedCells" type="xsd:boolean" use="optional" default="false"/>
# <xsd:attribute name="selectUnlockedCells" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="password" type="xsd:string" use="optional" default="nil"/>
# </xsd:complexType>

Expand Down
8 changes: 4 additions & 4 deletions test/workbook/worksheet/tc_sheet_view.rb
Expand Up @@ -197,11 +197,11 @@ def test_to_xml

doc = Nokogiri::XML.parse(@ws.sheet_view.to_xml_string)

assert_equal(1, doc.xpath("//sheetView[@tabSelected='false']").size)
assert_equal(1, doc.xpath("//sheetView[@tabSelected=0]").size)

assert_equal(1, doc.xpath("//sheetView[@tabSelected='false'][@showWhiteSpace='false'][@showOutlineSymbols='false'][@showFormulas='false']
[@rightToLeft='false'][@windowProtection='false'][@showZeros='true'][@showRuler='true']
[@showRowColHeaders='true'][@showGridLines='true'][@defaultGridColor='true']
assert_equal(1, doc.xpath("//sheetView[@tabSelected=0][@showWhiteSpace=0][@showOutlineSymbols=0][@showFormulas=0]
[@rightToLeft=0][@windowProtection=0][@showZeros=1][@showRuler=1]
[@showRowColHeaders=1][@showGridLines=1][@defaultGridColor=1]
[@zoomScale='100'][@workbookViewId='0'][@zoomScaleSheetLayoutView='0'][@zoomScalePageLayoutView='0']
[@zoomScaleNormal='0'][@view='pageBreakPreview']").size)
end
Expand Down

0 comments on commit 5ccab46

Please sign in to comment.