Skip to main content
Participant
May 8, 2023
Answered

How to use xlsx.extendscript.js

  • May 8, 2023
  • 1 reply
  • 1944 views

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

This topic has been closed for replies.
Correct answer Kasyan Servetsky

I have no idea what xlsx.extendscript.js is, but you can use this function to get data from Excel to InDesign.

1 reply

Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
May 9, 2023

I have no idea what xlsx.extendscript.js is, but you can use this function to get data from Excel to InDesign.

Participant
May 9, 2023

Thanks so much, I will try that

Peter Kahrel
Community Expert
Community Expert
May 10, 2023

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;
}

 


@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.