Method: Rods#setAttributes

Defined in:
lib/rods.rb

#setAttributes(cell, attributes) ⇒ Object

Merges style-attributes of given attribute-hash with current style of given cell. Checks, whether the resulting style already exists in the archive of created styles or creates and archives a new style. Applies the found or created style to cell. Cell is a REXML::Element.

mySheet.setAttributes(cell,{ "border-right" => "0.05cm solid magenta4",
                             "border-bottom" => "0.03cm solid lightgreen",
                             "border-top" => "0.08cm solid salmon",
                             "font-style" => "italic",
                             "font-weight" => "bold"})
mySheet.setAttributes(cell,{ "border" => "0.01cm solid turquoise", # turquoise frame
                             "text-align" => "center",             # center alignment
                             "background-color" => "yellow2",      # background-color
                             "color" => "blue"})                   # font-color
1.upto(7){ |row|
  cell=mySheet.getCell(row,5)
  mySheet.setAttributes(cell,{ "border-right" => "0.07cm solid green6" }) 
}



1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
# File 'lib/rods.rb', line 1287

def setAttributes(cell,attributes)
  die("setAttributes: cell #{cell} is not a REXML::Element") unless (cell.class.to_s == "REXML::Element")
  die("setAttributes: hash #{attributes} is not a hash") unless (attributes.class.to_s == "Hash")
  #----------------------------------------------------------------------
  # Flag, ob neue Attribute und deren Auspraegungen bereits im aktuellen
  # style vorhanden sind
  #----------------------------------------------------------------------
  containsMatchingAttributes=TRUE
  #-----------------------------------------------------------------------
  # Attribut-Hash, welcher "convenience"-Werte enthalten kann (und wird ;-) 
  # zunaechst normieren
  #-----------------------------------------------------------------------
  attributes=normStyleHash(attributes)
  die("setAttributes: attribute style:name not allowed in attribute-list as automatically generated") if (attributes.has_key?("style:name"))
  #------------------------------------------------------------------
  # Falls Zelle bereits style zugewiesen hat
  #------------------------------------------------------------------
  currentStyleName=cell.attributes["table:style-name"]
  if(currentStyleName)
    #---------------------------------------------------------------
    # style suchen (lassen)
    #---------------------------------------------------------------
    file,currentStyle=getStyle(currentStyleName)
    #-----------------------------------------------------------------------
    # Pruefung, ob oben gefundener style die neuen Attribute und deren Werte
    # bereits enthaelt.
    # Falls auch nur ein Attribut nicht oder nicht mit dem richtigen Wert
    # vorhanden ist, muss ein neuer style erstellt werden.
    # Grundannahme: Ein Open-Document-Style-Attribut kann per se immer nur in einem bestimmten Typ
    # Knoten vorkommen und muss daher nicht naeher qualifiziert werden !
    #-----------------------------------------------------------------------
    attributes.each{ |attribute,value|
      currentValue=currentStyle.attributes[attribute]
      #-------------------------------------------------
      # Attribut in Context-Node nicht gefunden ?
      #-------------------------------------------------
      if(! currentValue)  # nilClass
        tell("setAttributes: #{currentStyleName}: #{attribute} not in Top-Node")
        #-----------------------------------------------------------
        # Attribut mit passendem Wert dann in Kind-Element vorhanden ?
        #-----------------------------------------------------------
        if(currentStyle.elements["*[@#{attribute} = '#{value}']"])
          tell("setAttributes: #{currentStyleName}: #{attribute}/#{value} matching in Sub-Node")
        #-----------------------------------------------------------
        # andernfalls Komplettabbruch der Pruefschleife aller Attribute und Flag setzen
        # => neuer style muss erzeugt werden
        #-----------------------------------------------------------
        else
          tell("setAttributes: #{currentStyleName}: #{attribute}/#{value} not matching in Sub-Node")
          containsMatchingAttributes=FALSE
          break
        end
      #--------------------------------------------------
      # Attribut in Context-Node gefunden
      #--------------------------------------------------
      else
        #--------------------------------------------------
        # Passt der Wert des gefundenen Attributes bereits ?
        #--------------------------------------------------
        if (currentValue == value)
          tell("setAttributes: #{currentStyleName}: #{attribute}/#{value} matching in Top-Node")
        #-------------------------------------------------
        # bei unpassendem Wert Flag setzen
        #-------------------------------------------------
        else
          tell("setAttributes: #{currentStyleName}: #{attribute}/#{value} not matching with #{currentValue} in Top-Node")
          containsMatchingAttributes=FALSE
        end
      end
    }
    #--------------------------------------------------------
    # Wurden alle Attribut-Wertepaare gefunden, d.h. kann 
    # bisheriger style weiterverwendet werden ?
    #--------------------------------------------------------
    if(containsMatchingAttributes)
      tell("setAttributes: #{currentStyleName}: all attributes/values matching -> keeping current style")
    #-------------------------------------------------------
    # nein => passenden Style in Archiv suchen oder klonen und anpassen
    #-------------------------------------------------------
    else
      getAppropriateStyle(cell,currentStyle,attributes)
    end
  #------------------------------------------------------------------------
  # Zelle hatte noch gar keinen style zugewiesen
  #------------------------------------------------------------------------
  else
    #----------------------------------------------------------------------
    # Da style fehlt, ggf. aus office:value-type bestmoeglichen style ermitteln
    #----------------------------------------------------------------------
    valueType=cell.attributes["office:value-type"]
    if(valueType)
      case valueType
        when "string" then currentStyleName="myString"
        when "percentage" then currentStyleName="myPercentage"
        when "currency" then currentStyleName="myCurrency"
        when "float" then currentStyleName="myFloat"
        when "date" then currentStyleName="myDate"
        when "time" then currentStyleName="myTime"
      else
        die("setAttributes: unknown office:value-type #{valueType} found in #{cell}")
      end
    else
      #-----------------------------------------
      # 'myString' als Default
      #-----------------------------------------
      currentStyleName="myString" 
    end
    #-------------------------------------------------------
    # passenden Style in Archiv suchen oder klonen und anpassen
    #-------------------------------------------------------
    file,currentStyle=getStyle(currentStyleName)
    getAppropriateStyle(cell,currentStyle,attributes)
  end
end