1

I have a product catalog with 10.000+ products. The price is located in the cell next to SKU (see screenshot).

Indesign - Price next to SKU

I would like to update the price with data from a csv file. In the first row from the csv-file I have SKU and the second row the new price (see screenshot).

csv - SKU-Price

I thought I found a solution here. I tried to use the script, but I get an error at line 3 (mTarget = mDoc.stories.everyItem().tables.everyItem(),).

Any help is much appreciated. Thank you!

  • In excel index() with match(0 usually works. – Solar Mike Feb 01 '22 at 10:14
  • 1
    The variable `mTarget` don't even used in the script. You can just remove the line 3. Not sure if it's the only problem though, since you didn't provide your indd and csv files. A solution depends on how exactly your indd looks like inside. – Yuri Khristich Feb 01 '22 at 12:39
  • 1
    I have the working script (for a common case) but it makes no sense to post it. It looks like the asker doesn't need for answers at all. So the question can be closed. – Yuri Khristich Feb 02 '22 at 17:17
  • @YuriKhristich I thought that closing the question with "Any help is much appreciated. Thank you!" was clear enough that I am looking for an answer. When you can post the working script, than I'm very grateful. – DoubleUDoubleE Feb 03 '22 at 18:30
  • Did you try the suggested solution to remove line 3 from the old script? It's impossible to help you if you don't answer on comments, don't try suggested solutions and don't share a requested information. – Yuri Khristich Feb 03 '22 at 20:31
  • I just added my answer with my variant of the script. But I still doubt it makes sense. – Yuri Khristich Feb 03 '22 at 20:52

1 Answers1

0

My script is here:

// get data from the CSV file
var file = File('c:/scripts/id/update_from_csv/prices.csv');
file.open('r');
var data = file.read().split('\n');
file.close();

// convert the data into the object = {id1:price1, id2:price2, id3:price3, ...}
var obj = {};
for (var i = 0; i < data.length; i++) {
    var row = data[i].split(',');
    var id = row[0];
    var price = row[1];
    if (id != '') obj[id] = price;  // <------------------ here the update
}

// loop through IDs of the object and change cells in the document
var doc = app.activeDocument;
app.findTextPreferences = null;
for (var id in obj) {
    app.findTextPreferences.findWhat = id;
    var founds = doc.findText();
    for (var f = 0; f < founds.length; f++) {
        var cell = founds[f].texts[0].parent;
        if (cell.constructor.name == "Cell")
            cell.parent.cells[cell.index + 1].contents = obj[id];
    }
}

Here is my csv file prices.csv:

1234567890,100
1234567891,200
1234567892,50

Here are two screenshots from my indd file.

Before:

enter image description here

After:

enter image description here

Basically it works about in the same way as the script by the link in the question, except it handles all IDs, even if there are several identical IDs in the document.

But the error in your question tells me that you probably have no tables in your documents (!), in this case my code will not work.

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
  • There are a lot of tables in the INDD document, but your script is not working for me. I have made a new INDD document with the table from your example. Also the csv file is the same. It is still not working. I get an error in line 22 ( _italic_ var founds = doc.findText(); _italic_) – DoubleUDoubleE Feb 04 '22 at 13:28
  • As I've said the possible solution depends on how your INDD file look like. Share your file. It's impossible to guess what's going on out there. – Yuri Khristich Feb 04 '22 at 13:31
  • Here a link to a folder with the files https://drive.google.com/drive/folders/1Kaw9a5zWuwdavcjv_FNuu7YSNWN84zPw?usp=sharing – DoubleUDoubleE Feb 04 '22 at 13:39
  • Could you, please, save IDML file, since I have no the last version of InDesign at the hand? – Yuri Khristich Feb 04 '22 at 13:43
  • Try to change in your 'jsx' file the line `var row = data[i].split(',')` to `var row = data[i].split(';');` since you're using semi-colons `;` in your csv-file. (I used commas `,`) – Yuri Khristich Feb 04 '22 at 14:10
  • You can find the IDML file is in the same folder. I changed the comma into semi-colons as you asked, this will not help. I got the same error – DoubleUDoubleE Feb 04 '22 at 15:05
  • Well. Your CSV file contains the empty line at the end. It was the reason of the error. I've update one line in my code to ignore the empty lines in CSV. And look, your CSV contains the ID `1234567893` but INDD/IDML has ID `1234567892` in the last row. This ID will not updated from the CSV. – Yuri Khristich Feb 04 '22 at 16:20
  • Great it is working. Many thanks for your help – DoubleUDoubleE Feb 04 '22 at 19:11