Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

how to import data either via CSV, JSON or Spreadsheet

Explorer ,
Jan 21, 2020 Jan 21, 2020

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
3.0K
Translate
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

LEGEND , 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.

Translate
Adobe
Community Expert ,
Jan 21, 2020 Jan 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
Translate
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

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

 

Translate
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
LEGEND ,
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.

Translate
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
LATEST

Lumigraphics,

 

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

Translate
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