React Native
React Native
CSV Append New Row
Demonstrates how to append a new row to a CSV.Chilkat React Native Downloads
import { Csv, StringBuilder } from '@chilkat/react-native'
function chilkatExample() {
// We have the following CSV...
// permalink,company,numEmps,category,city,state,fundedDate,raisedAmt,raisedCurrency,round
// lifelock,LifeLock,,web,Tempe,AZ,1-May-07,6850000,USD,b
// mycityfaces,MyCityFaces,7,web,Scottsdale,AZ,1-Jan-08,50000,USD,seed
// flypaper,Flypaper,,web,Phoenix,AZ,1-Feb-08,3000000,USD,a
// infusionsoft,Infusionsoft,105,software,Gilbert,AZ,1-Oct-07,9000000,USD,a
// gauto,gAuto,4,web,Scottsdale,AZ,1-Jan-08,250000,USD,seed
const bCrlf = true;
const sb = new StringBuilder();
sb.appendLine('permalink,company,numEmps,category,city,state,fundedDate,raisedAmt,raisedCurrency,round', bCrlf);
sb.appendLine('lifelock,LifeLock,,web,Tempe,AZ,1-May-07,6850000,USD,b', bCrlf);
sb.appendLine('mycityfaces,MyCityFaces,7,web,Scottsdale,AZ,1-Jan-08,50000,USD,seed', bCrlf);
sb.appendLine('flypaper,Flypaper,,web,Phoenix,AZ,1-Feb-08,3000000,USD,a', bCrlf);
sb.appendLine('infusionsoft,Infusionsoft,105,software,Gilbert,AZ,1-Oct-07,9000000,USD,a', bCrlf);
sb.appendLine('gauto,gAuto,4,web,Scottsdale,AZ,1-Jan-08,250000,USD,seed', bCrlf);
const csv = new Csv();
csv.hasColumnNames = true;
csv.loadFromString(sb.getAsString());
// Let's say we want to append the following row:
// yelp,Yelp,,web,San Francisco,CA,1-Jul-04,1000000,USD,a
// If we have the row as-is, we could do this:
const csvTemp = new Csv();
csvTemp.loadFromString('yelp,Yelp,,web,San Francisco,CA,1-Jul-04,1000000,USD,a');
const numCols = csvTemp.numColumns;
let i = 0;
const row = csv.numRows;
while (i < numCols) {
csv.setCell(row, i, csvTemp.getCell(0, i));
i++;
}
// Show the updated CSV:
console.log(csv.saveToString());
// Output should be:
// permalink,company,numEmps,category,city,state,fundedDate,raisedAmt,raisedCurrency,round
// lifelock,LifeLock,,web,Tempe,AZ,1-May-07,6850000,USD,b
// mycityfaces,MyCityFaces,7,web,Scottsdale,AZ,1-Jan-08,50000,USD,seed
// flypaper,Flypaper,,web,Phoenix,AZ,1-Feb-08,3000000,USD,a
// infusionsoft,Infusionsoft,105,software,Gilbert,AZ,1-Oct-07,9000000,USD,a
// gauto,gAuto,4,web,Scottsdale,AZ,1-Jan-08,250000,USD,seed
// yelp,Yelp,,web,San Francisco,CA,1-Jul-04,1000000,USD,a
}