PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Ruby and Excel
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Ruby and Excel

Réponse
 
LinkBack Outils de la discussion
Vieux 14/09/2007, 23h18   #1 (permalink)
Analogy Analogy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Ruby and Excel

I'm reading an excel file (text and numbers) with the intent of storing
the data in a 2D array.


Here's my code to read in the data (after opening the excel file):


line = '1'

array = []

while worksheet.Range("c"+line) ['Value'] do

row = []

for column in 'c'..'z' do

row <<worksheet.cells(line, column).text

end

line.succ!

array << row


My problem is that this works if the first two columns of the excel file
are empty (i.e. data starts in cell C1), but doesn't read the excel data
if it starts in the first or second row (i.e. cell A1). Anyone know how
I might be able to pull all my data into a 2D array no matter where the
data starts? Thanks!
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 15/09/2007, 02h35   #2 (permalink)
mully
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby and Excel

On Sep 14, 6:18 pm, Analogy Analogy <analog...@aol.com> wrote:
> I'm reading an excel file (text and numbers) with the intent of storing
> the data in a 2D array.
>
> Anyone know how
> I might be able to pull all my data into a 2D array no matter where the
> data starts? Thanks!
>


Here's a method I frequently use to get a 2-dimensional array of all
values from a worksheet...

data = worksheet.UsedRange.Value

David

http://rubyonwindows.blogspot.com/search/label/excel

  Réponse avec citation
Vieux 17/09/2007, 15h30   #3 (permalink)
Analogy Analogy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby and Excel

Thanks for the suggestion David. How would I get rid of the "nil" values
for the empty cells that are pulled into the 2D array? Thanks!!
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 18/09/2007, 02h54   #4 (permalink)
David Mullet
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby and Excel

Analogy Analogy wrote:
> Thanks for the suggestion David. How would I get rid of the "nil" values
> for the empty cells that are pulled into the 2D array? Thanks!!


How you do this may depend on what you plan do with your data, and when.

I'm assuming you want to preserve the structure; otherwise the
aforementioned compact method will remove nil values for you.

I frequently convert nils to empty strings with the to_s method. You may
wish to do this when outputting your data, or in-place using a method
such as collect!:

data = worksheet.UsedRange.Value

=> [["A1", "B1", "C1", nil, "E1"], ["A2", nil, "C2", "D2", nil], [nil,
"B3", nil, "D3", "E3"]]

data.collect!{|row| row.collect!{|field| field.to_s}}

=> [["A1", "B1", "C1", "", "E1"], ["A2", "", "C2", "D2", ""], ["", "B3",
"", "D3", "E3"]]

If it's numeric data, perhaps to_i or to_f works better for you.

Hope that s.

David

http://rubyonwindows.blogspot.com
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h17.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11958 seconds with 12 queries