Copy link to clipboard
Copied
Hey i am trying to read from an Excel file and I am using (#include "xlsx.extendscript.js"; ). This not working. I tried using seeing if I could use #include. It says Private identifiers are not allowed outside class bodies.
Need help to know how to correctly use xlsx
I have no idea what xlsx.extendscript.js is, but you can use this function to get data from Excel to InDesign.
Copy link to clipboard
Copied
I have no idea what xlsx.extendscript.js is, but you can use this function to get data from Excel to InDesign.
Copy link to clipboard
Copied
Thanks so much, I will try that
Copy link to clipboard
Copied
Hi @Victor29751988fsbx , Another approach is to temporarily place the excel file and get its contents as a nested array:
var xlsf = File.openDialog("Please Choose an Excel file");
alert("Contents of row 2 cell 1: " + getExcelArray(xlsf)[1][0]);
/**
* Coverts an Excel file into a nested array
* @ param Excel File to place
* @ returns the Excel table as a nested array
*
*/
function getExcelArray(xf){
var doc = app.activeDocument;
var xta = [];
var temp = doc.pages[0].textFrames.add();
try {
temp.place(xf);
var xtr = temp.parentStory.tables[0].rows.everyItem().getElements();
}catch(e) {
temp.remove()
alert(e)
}
var c;
for (var i = 1; i < xtr.length; i++){
c = xtr[i].cells.everyItem().getElements();
var ca = [];
for (var j = 0; j < c.length; j++){
ca.push(c[j].texts[0].contents)
};
xta.push(ca)
};
temp.remove()
return xta;
}
Copy link to clipboard
Copied
@rob day -- There's a shortcut to getting a table's content as an array of arrays:
xta = temp.parentStory.tables[0].rows.everyItem().contents;
P.
Copy link to clipboard
Copied
Hi Peter, I was hoping that would work, but when I try it in my function the array elements are empty—a writeIn for xta returns ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
function getExcelArray(xf){
var doc = app.activeDocument;
var temp = doc.pages[0].textFrames.add();
try {
temp.place(xf);
var xta = temp.parentStory.tables[0].rows.everyItem().contents;
$.writeln(xta)
//returns ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
}catch(e) {
temp.remove()
alert(e)
}
temp.remove()
return xta;
}
Copy link to clipboard
Copied
Turns out that when the table is exposed it works as expected. Just make the frame very big and enable ignoreWrap to make sure that another frame doesn;t cause overset:
var temp = doc.pages[0].textFrames.add ({
geometricBounds: [0,0,1000,1000],
textFramePreferences: {
ignoreWrap: true,
},
});