export to .csv

118 views
Skip to first unread message

Antonio P

unread,
Jan 23, 2018, 3:45:19 AM1/23/18
to mementodatabase
Hola buenos dias,
Puede alguien ayudarme??
necesitaria un script para exportar todos los dias las entradas de mi biblioteca a un archivo .csv

muchas gracias

Bill Crews

unread,
Jan 23, 2018, 4:16:12 AM1/23/18
to Antonio P, mementodatabase
Why a script? Why not just expert your library to a CSV file every day?

Bill Crews

unread,
Jan 23, 2018, 11:23:32 AM1/23/18
to Antonio P, mementodatabase
The first thing that occurs to me is to avoid export by syncing the libraries to Sheets. This way, you have to do nothing each day, but the libraries remain "live"; the data continues to change, and so do the sheets after 7am, which may be a problem for you.

The second idea would be a library action in each library with 22 distinct scripts. The scripts will be the same on front and back, but the statements in the middle that emit the CSV data will match the defined fields in each library, which are presumably distinct from each other.

To do this, start with one library, La. From La's entries list, press 3-dot menu and then Scripts, then Action, set to Library, and give it a name, say Sa. Here's the script...

// Prelude
// Create file
var filename = "FileA.csv";
var csv = file(filename);
If (csv == null) {
   message("Can't create file " + filename);
   exit();
   }
// Write header line
csv.write('"Field1"', '"Field2"', ... , '"Fieldn"');
csv.writeLine();
// Generate data lines
var entries = lib().entries();
for (ent in entries) {
   var e = entries[ent];
   csv.write('"' + e.field("Field1") + '"');
   csv.write(', "' + e.field("Field2") + '"');
   ...
   csv.writeLine();
   }
// Postlude: close file & end
csv.close();
message("File " + filename + " created");

Note that you may use a pair of single-quotes or a pair of double-quotes. If you use one pair within the other pair, the inner ones are part of the data written to the file. So, parse carefully the quotes in the script.

So, this takes care of file FileA.csv for library La with script Sa. The actual field names must be substituted for Field1, Field2, etc. The ellipses (...) are for you to fill in with copy/paste and fixup, based on each library's structure definition.

There is also an issue that there are multiple CSV file formats, though they don't vary a lot. Some expect all fields values to be double-quoted, while some want numbers to be without double-quotes. I leave dealing with that to you, based on the software you are using on your platforms.

Alter the script for Sb..Sv as needed for libraries Lb..Lv.

Antonio P

unread,
Jan 29, 2018, 8:05:25 AM1/29/18
to mementodatabase
Hola tengo una duda..
cuando creo el archivo csv el campo fecha tiene un formato largo "viernes 26 de enero de 2018"

Puedo ponerlo en formato corto "YYYY/MM/DD" por ejemplo??

var filename = "Revisión VN_for_send.csv";
var csv = file(filename);
if (csv == null) {
   message("No hay registros " + filename);
   exit();
   }
// Write header line
csv.write('Fecha', ',ZONA', ',Inspector Lote', ',Observaciones', ',Chasis 1', ',Chasis 2', ',Chasis 3', ',Chasis 4', ',Chasis 5', ',Chasis 6', ',Chasis 7', ',Chasis 8', ',Chasis 9', ',Chasis 10', ',Incidencias en los VN', ',Revision completa', ',Retirado de la carga 1', ',Defecto 1', ',foto defecto', ',Retirado de la carga 2', ',Defecto 2', ',foto defecto 2');
csv.writeLine();
// Generate data lines
var entries = lib().entries();
for (ent in entries) {
   var e = entries[ent];
   csv.write('"' + e.field("Fecha").format('YYYY/MM/DD') + '"'); (((--esto no funciona---)))
   csv.write(',"' + e.field("ZONA") + '"');........

Gracias otra vez!!

Bill Crews

unread,
Jan 29, 2018, 9:06:33 AM1/29/18
to Antonio P, mementodatabase
Yes, dates are not handled like anything else; it is very unfortunate. This worked for me...

var filename = "Revisión VN_for_send.csv";
var csv = file(filename);
if (csv == null) {
   message("No hay registros " + filename);
   exit();
   }
// Write header line
csv.write('Fecha', ',ZONA', ',Inspector Lote', ',Observaciones', ',Chasis 1', ',Chasis 2', ',Chasis 3', ',Chasis 4', ',Chasis 5', ',Chasis 6', ',Chasis 7', ',Chasis 8', ',Chasis 9', ',Chasis 10', ',Incidencias en los VN', ',Revision completa', ',Retirado de la carga 1', ',Defecto 1', ',foto defecto', ',Retirado de la carga 2', ',Defecto 2', ',foto defecto 2');
csv.writeLine();
// Generate data lines
var entries = lib().entries();
for (ent in entries) {
   var e = entries[ent];
   // csv.write('"' + e.field("Fecha").format('YYYY/MM/DD') + '"'); (((--esto no funciona---)))
   csv.write('"' + moment(e.field("Fecha")).format('YYYY/MM/DD') + '"');
   csv.write(',"' + e.field("ZONA") + '"');........

Make sure in the field edit card to press Add JavaScript libraries and then check moment.min.js.
Reply all
Reply to author
Forward
0 new messages