Method: QuickBase::Client#makeSVFile

Defined in:
lib/QuickBaseClient.rb

#makeSVFile(filename, fieldSeparator = ",", dbid = @dbid, query = nil, qid = nil, qname = nil) ⇒ Object

Make a CSV file using the results of a query. Specify a different separator in the second paramater. Fields containing the separator will be double-quoted.

e.g. makeSVFile( “contacts.txt”, “\t”, nil ) e.g. makeSVFile( “contacts.txt”, “,”, “dhnju5y7”, nil, nil, “List Changes” )



4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
# File 'lib/QuickBaseClient.rb', line 4453

def makeSVFile( filename, fieldSeparator = ",", dbid = @dbid, query = nil, qid = nil, qname = nil )
   File.open( filename, "w" ) { |file|

      if dbid
         doQuery( dbid, query, qid, qname )
      end

      if @records and @fields

         # ------------- write field names on first line ----------------
         output = ""
         fieldNamesBlock = proc { |element|
            if element.is_a?(REXML::Element) and element.name == "label" and element.has_text?
               output << "#{element.text}#{fieldSeparator}"
            end
         }
         processChildElements( @fields, true, fieldNamesBlock )

         output << "\n"
         output.sub!( "#{fieldSeparator}\n", "\n" )
         file.write( output )

         # ------------- write records ----------------
         output = ""
         valuesBlock = proc { |element|
            if element.is_a?(REXML::Element)
               if element.name == "record"
                  if output.length > 1
                     output << "\n"
                     output.sub!( "#{fieldSeparator}\n", "\n" )
                     file.write( output )
                  end
                  output = ""
               elsif  element.name == "f"
                  if  element.has_text?
                     text = element.text
                     text.gsub!("<BR/>","\n")
                     text = "\"#{text}\"" if text.include?( fieldSeparator )
                     output << "#{text}#{fieldSeparator}"
                  else
                     output << "#{fieldSeparator}"
                  end
               end
            end
         }

         processChildElements( @records, false, valuesBlock )
         if output.length > 1
           output << "\n"
           output.sub!( "#{fieldSeparator}\n", "\n" )
           file.write( output )
           output = ""
         end
      end
   }
end