0
Read the CSV file multiple columns using for loop
Contributor
,
/t5/indesign-discussions/read-the-csv-file-multiple-columns-using-for-loop/td-p/12319488
Aug 16, 2021
Aug 16, 2021
Copy link to clipboard
Copied
Hi All,
I'm using the below functions to read the CSV file, I have able to read the rows using loop and my requirement is need to loop the columns, in my CSV I have around 50-60 columns and need to fetch the data each columns wise instead of hard code the column number is it possible?
GettingInputFromCSV_UI(File(AssetCSV_File.fullName));
if(Replacement_Content_csv.length>0){//Replacement_Content_csv // Rules_styleMapping_csv
for(var a=0;a<Replacement_Content_csv.length;a++){
FolderNames.push(String(Replacement_Content_csv[a][0]));
SubFolderNames.push(String(Replacement_Content_csv[a][1]));
if(Rules_styleMapping_head_csv.length>0){
for(var b=0;b<Rules_styleMapping_head_csv.length;b++){
if(String(Rules_styleMapping_head_csv[b]) != undefined && String(Rules_styleMapping_head_csv[b]) != ""){
if(String(Rules_styleMapping_head_csv[b]) == "Test1") {
ReadCSV_ColumnsTHTH.push(Replacement_Content_csv[a][2]);
}
if(String(Rules_styleMapping_head_csv[b]) == "Test2") {
ReadCSV_ColumnsUSEN.push(Replacement_Content_csv[a][3]);
}
if(String(Rules_styleMapping_head_csv[b]) == "Test3") {
ReadCSV_ColumnsWWEN.push(Replacement_Content_csv[a][4]);
}
}
}
}
}
}
function GettingInputFromCSV_UI(fs){
var csvFLG = 0;
var LineCount = 0;
fs.open("r");
zRead =fs.readln(); //heading
var HeadCommareplace = String(zRead).replace(", ","<COMMA>","g");
Rules_styleMapping_head_csv = HeadCommareplace.split(",");
ReadCSV_ColumnsHeads.push(Rules_styleMapping_head_csv);
while(fs.eof == false){
zRead =fs.readln();
if(zRead.length != 0){
zRead = zRead.replace(", ","<COMMA>","g");
var LineArr = zRead.split(",");
var LineStr = "";
LineStr = LineArr.join("");
if(LineStr != ""){
Replacement_Content_csv.push(LineArr);
}
}
}
fs.close();
}
TOPICS
Scripting
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
LATEST
/t5/indesign-discussions/read-the-csv-file-multiple-columns-using-for-loop/m-p/12321562#M441498
Aug 17, 2021
Aug 17, 2021
Copy link to clipboard
Copied
You already know the column count at various stages, e.g. Rules_styleMapping_head_csv.
Your code seems over-elaborate. Instead of this:
var LineStr = "";
LineStr = LineArr.join("");
if(LineStr != ""){
(to check whether a string has any commas) you could use this:
if (LineArr.length > 1)
Or better yet, replace this:
var LineArr = zRead.split(",");
var LineStr = "";
LineStr = LineArr.join("");
if(LineStr != ""){
Replacement_Content_csv.push(LineArr);
}
with this:
if(zRead.indexOf(',') > -1){
Replacement_Content_csv.push(zRead.split(","));
}
P.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

