module Xlsxrb::Ooxml::Writer::DrawingXml
Mixin containing DrawingML, Chart, Shape, Comment, and VML XML generation logic.
Constants
- THREADED_COMMENTS_NS
Public Instance Methods
(untyped cell_ref) → untyped
Source
# File lib/xlsxrb/ooxml/writer/drawing_xml.rb, line 182 def cell_to_col_row(cell_ref) m = cell_ref.match(/\A([A-Z]+)(\d+)\z/) return [0, 0] unless m col = column_letter_to_index(m[1]) - 1 row = m[2].to_i - 1 [col, row] end
(?untyped val) → untyped
Source
# File lib/xlsxrb/ooxml/writer/drawing_xml.rb, line 195 def format_guid(val = nil) return "{#{SecureRandom.uuid.upcase}}" if val.nil? || val.to_s.empty? str = val.to_s.strip str = "{#{str}}" unless str.start_with?("{") && str.end_with?("}") str.upcase end
Formats or generates a GUID in standard uppercase braced format {UUID}. : (?String? val) -> String
(untyped chart) → untyped
Source
# File lib/xlsxrb/ooxml/writer/drawing_xml.rb, line 44 def generate_chart_xml(chart) # Charts read from files carry the element name ("lineChart") as their type. chart_type = CHART_TYPE_MAP[chart[:type]] || CHART_TYPE_MAP.values.find { |element| element == chart[:type].to_s } || "barChart" XmlBuilder.build(declaration: :plain) do |xml| xml.tag("c:chartSpace", { "xmlns:c": C_NS, "xmlns:a": A_NS, "xmlns:r": DOC_REL_NS }) do xml.empty_tag("c:roundedCorners", { val: chart[:rounded_corners] ? 1 : 0 }) unless chart[:rounded_corners].nil? xml.empty_tag("c:style", { val: chart[:style] }) if chart[:style] if (prot = chart[:protection]) xml.tag("c:protection") do xml.empty_tag("c:chartObject", { val: prot[:chart_object] ? 1 : 0 }) unless prot[:chart_object].nil? xml.empty_tag("c:data", { val: prot[:data] ? 1 : 0 }) unless prot[:data].nil? xml.empty_tag("c:formatting", { val: prot[:formatting] ? 1 : 0 }) unless prot[:formatting].nil? xml.empty_tag("c:selection", { val: prot[:selection] ? 1 : 0 }) unless prot[:selection].nil? xml.empty_tag("c:userInterface", { val: prot[:user_interface] ? 1 : 0 }) unless prot[:user_interface].nil? end end xml.tag("c:chart") do write_chart_top_title_xml(xml, chart) xml.empty_tag("c:autoTitleDeleted", { val: chart[:auto_title_deleted] ? 1 : 0 }) unless chart[:auto_title_deleted].nil? write_view_3d_xml(xml, chart[:view_3d]) if chart[:view_3d] write_chart_walls_xml(xml, chart) write_plot_area_xml(xml, chart, chart_type) write_chart_legend_xml(xml, chart[:legend]) xml.empty_tag("c:plotVisOnly", { val: chart[:plot_vis_only] ? 1 : 0 }) unless chart[:plot_vis_only].nil? xml.empty_tag("c:dispBlanksAs", { val: chart[:disp_blanks_as] }) if chart[:disp_blanks_as] xml.empty_tag("c:showDLblsOverMax", { val: chart[:show_d_lbls_over_max] ? 1 : 0 }) unless chart[:show_d_lbls_over_max].nil? end if chart[:chart_fill] || chart[:chart_no_fill] || chart[:chart_line_color] || chart[:chart_line_dash] write_simple_sp_pr( xml, fill_color: chart[:chart_fill], no_fill: chart[:chart_no_fill], line_color: chart[:chart_line_color], line_width: chart[:chart_line_width], line_dash: chart[:chart_line_dash] ) end write_axis_txpr(xml, nil, chart[:chart_font]) if chart[:chart_font] write_chart_print_settings_xml(xml, chart[:print_settings]) if chart[:print_settings] end end end
: (untyped chart) -> ::String
(untyped sheet_comments) → untyped
Source
# File lib/xlsxrb/ooxml/writer/drawing_xml.rb, line 92 def generate_comments_xml(sheet_comments) authors = sheet_comments.map { |c| c[:author] }.uniq XmlBuilder.build(declaration: :plain) do |xml| xml.tag("comments", { xmlns: SSML_NS }) do xml.tag("authors") do authors.each { |a| xml.text_tag("author", a || "") } end xml.tag("commentList") do sheet_comments.each do |c| aid = authors.index(c[:author]) || 0 xml.tag("comment", { ref: c[:ref] || "", authorId: aid, guid: c[:guid], shapeId: c[:shape_id] }) do xml.tag("text") do if c[:text].is_a?(Xlsxrb::Elements::RichText) write_rich_text_xml(xml, c[:text]) else xml.tag("r") { xml.text_tag("t", c[:text] || "") } end end end end end end end end
: (untyped sheet_comments) -> ::String
(untyped rels_data) → untyped
Source
# File lib/xlsxrb/ooxml/writer/drawing_xml.rb, line 29 def generate_drawing_rels(rels_data) XmlBuilder.build(declaration: :plain) do |xml| xml.tag("Relationships", { xmlns: REL_NS }) do rels_data.each_with_index do |rel, i| rel_type = case rel[:type] when :image then "#{DOC_REL_NS}/image" when :chart then "#{DOC_REL_NS}/chart" end xml.empty_tag("Relationship", { Id: "rId#{i + 1}", Type: rel_type || "", Target: rel[:target] || "" }) end end end end
: (untyped rels_data) -> ::String
(untyped drawing_parts) → untyped
Source
# File lib/xlsxrb/ooxml/writer/drawing_xml.rb, line 11 def generate_drawing_xml(drawing_parts) XmlBuilder.build(declaration: :plain) do |xml| xml.tag("xdr:wsDr", { "xmlns:xdr": XDR_NS, "xmlns:a": A_NS, "xmlns:r": DOC_REL_NS }) do drawing_parts.each do |dp| case dp[:kind] when :pic write_pic_anchor_xml(xml, dp) when :chart write_chart_anchor_xml(xml, dp) when :sp write_shape_anchor_xml(xml, dp) end end end end end
: (untyped drawing_parts) -> ::String
(untyped persons) → untyped
Source
# File lib/xlsxrb/ooxml/writer/drawing_xml.rb, line 239 def generate_persons_xml(persons) XmlBuilder.build(declaration: :plain) do |xml| xml.tag("personList", { xmlns: THREADED_COMMENTS_NS }) do persons.each do |p| p_id = p[:id] || format_guid(nil) name = p[:display_name] || p[:name] || "Author" user_id = p[:user_id] || "AD:#{name}" provider_id = p[:provider_id] || "AD" xml.empty_tag("person", { displayName: name, id: p_id, userId: user_id, providerId: provider_id }) end end end end
Generates XML for xl/persons/person.xml. : (Array[Hash[Symbol, untyped]] persons) -> String
(untyped sheet_threaded_comments) → untyped
Source
# File lib/xlsxrb/ooxml/writer/drawing_xml.rb, line 205 def generate_threaded_comments_xml(sheet_threaded_comments) XmlBuilder.build(declaration: :plain) do |xml| xml.tag("ThreadedComments", { xmlns: THREADED_COMMENTS_NS }) do sheet_threaded_comments.each do |c| ref = c[:ref] || c[:cell] dt = c[:timestamp] || c[:dt] dt_str = case dt when Time, DateTime dt.to_time.utc.strftime("%Y-%m-%dT%H:%M:%SZ") when String dt else Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ") end person_id = c[:person_id] || c[:author_id] || "{00000000-0000-0000-0000-000000000000}" comment_id = c[:id] || format_guid(nil) attrs = { ref: ref || "", dT: dt_str, personId: person_id, id: comment_id, parentId: c[:parent_id], done: ([true, "true", "1"].include?(c[:done]) ? "true" : nil) } xml.tag("threadedComment", attrs) do xml.text_tag("text", c[:text].to_s) end end end end end
Generates XML for xl/threadedComments/threadedComment{N}.xml. : (Array[Hash[Symbol, untyped]] sheet_threaded_comments) -> String
(untyped sheet_comments) → untyped
Source
# File lib/xlsxrb/ooxml/writer/drawing_xml.rb, line 118 def generate_vml_drawing_xml(sheet_comments) XmlBuilder.build do |xml| xml.tag( "xml", { "xmlns:v": "urn:schemas-microsoft-com:vml", "xmlns:o": "urn:schemas-microsoft-com:office:office", "xmlns:x": "urn:schemas-microsoft-com:office:excel" } ) do xml.tag("o:shapelayout", { "v:ext": "edit" }) do xml.empty_tag("o:idmap", { "v:ext": "edit", data: 1 }) end xml.tag( "v:shapetype", { id: "_x0000_t202", coordsize: "21600,21600", "o:spt": 202, path: "m,l,21600r21600,l21600,xe" } ) do xml.empty_tag("v:stroke", { joinstyle: "miter" }) xml.empty_tag("v:path", { gradientshapeok: "t", "o:connecttype": "rect" }) end sheet_comments.each_with_index do |c, idx| col, row = cell_to_col_row(c[:ref]) shape_id = 1025 + idx style = "position:absolute;margin-left:59.25pt;margin-top:1.5pt;width:108pt;height:59.25pt;z-index:#{idx + 1};visibility:#{c[:visible] ? "visible" : "hidden"}" xml.tag( "v:shape", { id: "_x0000_s#{shape_id}", type: "#_x0000_t202", style: style, fillcolor: "#ffffe1", "o:insetmode": "auto" } ) do xml.empty_tag("v:fill", { color2: "#ffffe1" }) xml.empty_tag("v:shadow", { on: "t", color: "black", obscured: "t" }) xml.empty_tag("v:path", { "o:connecttype": "none" }) xml.tag("v:textbox", { style: "mso-direction-alt:auto" }) do xml.open_tag("div", { style: "text-align:left" }) xml.close_tag("div") end xml.tag("x:ClientData", { ObjectType: "Note" }) do xml.empty_tag("x:MoveWithCells") xml.empty_tag("x:SizeWithCells") anchor = c[:anchor] || Xlsxrb::Elements::Comment.default_anchor(col, row) xml.text_tag("x:Anchor", anchor.join(", ")) xml.text_tag("x:AutoFill", "False") xml.text_tag("x:Row", row) xml.text_tag("x:Column", col) xml.empty_tag("x:Visible") if c[:visible] end end end end end end
: (untyped sheet_comments) -> ::String