Skip to main content
davidc88034496
Known Participant
January 21, 2020
Answered

how to import data either via CSV, JSON or Spreadsheet

  • January 21, 2020
  • 2 replies
  • 3366 views

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)

This topic has been closed for replies.
Correct answer Lumigraphics

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.

2 replies

LumigraphicsCorrect answer
Brainiac
January 21, 2020

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.

davidc88034496
Known Participant
January 21, 2020

Lumigraphics,

 

thank you! i included a snippet of code that worked for me in the post above

JJMack
Community Expert
January 21, 2020

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   

JJMack
davidc88034496
Known Participant
January 21, 2020

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