Indesign - 如何使用来自 csv 的数据更新单元格中的值?

Indesign - How to update value in cell with data from csv?

我有一个包含 10.000 多种产品的产品目录。价格位于 SKU 旁边的单元格中(参见屏幕截图)。

Indesign - Price next to SKU

我想用 csv 文件中的数据更新价格。在 csv 文件的第一行我有 SKU,第二行是新价格(见截图)。

csv - SKU-Price

我以为我找到了解决方案 。我尝试使用该脚本,但在第 3 行出现错误 (mTarget = mDoc.stories.everyItem().tables.everyItem(),).

非常感谢任何帮助。谢谢!

我的脚本在这里:

// 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];
    }
}

这是我的 csv 文件 prices.csv:

1234567890,100
1234567891,200
1234567892,50

这是我的 indd 文件中的两个屏幕截图。

之前:

之后:

基本上它的工作方式与问题中的 相同,除了它处理所有 ID,即使文档中有多个相同的 ID。

但是你问题中的错误告诉我你的文档中可能没有表格 (!),在这种情况下我的代码将无法工作。