how to import data either via CSV, JSON or Spreadsheet

Explorer ,
Jan 21, 2020 Jan 21, 2020

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)

TOPICS
Actions and scripting, Import and export

Views

442

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Jan 21, 2020 Jan 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.

Likes

translate

Translate

Translate
Most Valuable Participant ,
Jan 21, 2020 Jan 21, 2020

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   

JJMack

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 21, 2020 Jan 21, 2020

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

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 21, 2020 Jan 21, 2020

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.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 21, 2020 Jan 21, 2020

Copy link to clipboard

Copied

Lumigraphics,

 

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

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines