I have a product catalog with 10.000+ products. The price is located in the cell next to SKU (see screenshot).
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).
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!
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:
After:
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.