I'm trying to import a CSV into an InDesign document using basil.js. However, depending on the dataset, I will often get an error of:
Javascript Error!
Error Number: 21
Error String: undefined object is not an object
Engine: main
File: /Users/...
includes/core.js
Line: 137 Source:
app.doScript(function() {
My script is based on this Working with CSV files demo except I have modified it so only one row of data is presented per line, and it inserts a new page every 4 rows. This is my script:
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
// load & convert
var data = b.CSV.decode( b.loadString("donorsopenS.csv") );
for (var i = 0; i < data.length; i++) {
data[i].Zip = parseInt(data[i].Zip);
};
// text settings
b.textSize(12);
b.textFont('Helvetica');
b.textAlign(Justification.LEFT_ALIGN);
b.units(b.MM);
var i = 0;
var verticalUnitSize = 20;
var horizontalUnitSize = 50;
for ( var i = 0; i < data.length; i++ ) {
for (var y = 0; y < 4; y++) {
var posX = horizontalUnitSize;
var posY = y*(verticalUnitSize);
var Contributor = data[i].Contributor;
var PositionBoard = data[i].PositionBoard;
var Amount = data[i].Amount;
var Recipient = data[i].Recipient;
b.text(Contributor, 0, posY,50,20);
b.text(PositionBoard, posX, posY,50,20);
b.text(Amount, posX*2, posY,50,20);
b.text(Recipient, posX*3, posY,50,20);
// stop drawing if no more rows are available
if (i > data.length) break;
i++;
};
// add new page
if (i < data.length-1) {
b.addPage();
}
}
}
b.go();
It works fine with a dataset of 10 rows – here is a sample dataset, but when it's a different number of rows it will return the error. Even in this case, if i were to change the for loop so 5 rows display per page it breaks.
Any help is much appreciated. Thank you!