Copy link to clipboard
Copied
I need a way to input data from a CSV/EXCEL spreadsheet into Adobe Photoshop JavaScript scripts. It can be either from EXCEL or an HTML TABLE web application.
I cannot find a decent snippet of code that imports data from a CSV.
I cannot find a spreadsheet application that converts a spreadsheet data into JSON.
if i could find a way to import data properly from an external source such as a JSON file or CSV file then that would be great. The only issue i am having with JSON is that there arent any good "spreadsheet" type of programs that will export as JSON.
Could someone recommend me the best solution for this (least cumbersome)
1 Correct answer
This is a common JS function and easy to implement. Have you looked on Stack Exchange?
Open the file, set a variable to the contents, split to put the fields in an array.
Explore related tutorials & articles
Copy link to clipboard
Copied
A CSV file is a text file a javascript should have no problem writing and reading a text file. Do you mean something different than reading the file when you use the term import data. If so perhaps you want to use Photoshop Data Driven Graphics feature. Creating data driven graphics
Copy link to clipboard
Copied
Thank you very much i was able to find a snippet of code that worked for me. I'll place it below incase someone wants to know. But yes it seems like you have to just read the file and use the .split() method to split by line and by commas in a Comma Seperated Value (.CSV) file. It wasnt too hard to find out after. Thanks for your help
var scriptRoot = app.activeDocument.path;
var fileCsv = File(scriptRoot + "/data.csv");
function csv2obj() {
fileCsv.encoding = "utf-8";
fileCsv.lineFeed = 'Macintosh';
fileCsv.open('r', undefined, undefined);
var content = fileCsv.read();
fileCsv.close();
var lines = content.split('\n');
var csvArrObjs = [];
var keys = lines[0].split(',')
for (var i = 1; i < lines.length; i++) {
var cells = lines[i].split(',');
var obj = {};
for (var k = 0; k < cells.length; k++) {
obj[keys[k]] = cells[k].replace(/\s*/gm, '');
}
csvArrObjs.push(obj);
}
return csvArrObjs;
}
var csvObj = csv2obj(fileCsv);
Copy link to clipboard
Copied
This is a common JS function and easy to implement. Have you looked on Stack Exchange?
Open the file, set a variable to the contents, split to put the fields in an array.
Copy link to clipboard
Copied
Lumigraphics,
thank you! i included a snippet of code that worked for me in the post above

