使用脚本打开Indesign文件并从文件夹中的Excel文档中更新表数据

发布于 2025-02-07 10:01:10 字数 164 浏览 1 评论 0原文

我的公司制作的数据报告经常被复制几百次。我创建了一个文档,其中包含附加到Excel文件的表数据,并且可以通过链接面板进行更新。我的问题是,是否有一种方法可以通过运行脚本来更新表中的数据,指向同一文件夹中的Excel文件?

文件中需要完成更复杂的操作,但这将是一个很好的起点!

先感谢您!

My company makes data reports that are often replicated a couple hundred times. I've created a document that has table data attached to an excel file and can be updated through the links panel. My question is, is there a way to update the data in the tables just by running a script, pointing to the excel file within the same folder?

There are more complicated actions that need to be completed in the file, but this would be a great starting point!

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌 2025-02-14 10:01:11

这是从剪贴板中获取INDD文件列表(完整路径!)的脚本,打开它们,更新链接,保存PDF(在具有与打开的文档相同名称的同一文件夹中),保存并关闭Indd文件:

var settings = save_setting();

var list = get_list_from_cliboard();

while (list.length) {
    var doc = app.open(list.shift());
    update_links(doc);
    save_pdf(doc);
    doc.close(SaveOptions.YES);
}

restore_settings(settings);


// functions ------------------------------------------------------------------

function get_list_from_cliboard() {
    var temp_doc = app.documents.add();
    app.paste();
    try { var contents = temp_doc.pages[0].textFrames[0].parentStory.contents.split('\r') }
    catch(e) { var contents = [] }
    temp_doc.close(SaveOptions.NO);
    return contents;
}

function update_links(doc) {
    var links = doc.links.everyItem().getElements();
    while(links.length) {
        var link = links.pop();
        if (link.status == LinkStatus.LINK_OUT_OF_DATE) link.update();
    }
}

function save_pdf(doc) {
    var name = doc.name.replace(/\.indd/g, '.pdf');
    var file = File(doc.fullName.parent + '/'  + name);
    var preset = app.pdfExportPresets.item("[High Quality Print]");
    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, file, false, preset);
}

function save_setting() {
    var settings = {
        checkLinksAtOpen: app.linkingPreferences.checkLinksAtOpen,
        viewPDF: app.pdfExportPreferences.viewPDF,
    };
    app.linkingPreferences.checkLinksAtOpen = false;
    app.pdfExportPreferences.viewPDF = false;
    return settings;
}

function restore_settings(settings) {
    app.linkingPreferences.checkLinksAtOpen = settings.checkLinksAtOpen;
    app.pdfExportPreferences.viewPDF = settings.viewPDF;
}

您可以复制在Excel中,INDD文件的路径列表运行脚本并获取PDF文件和更新的INDD文件。

Here is the script that takes list of INDD files (full paths!) from the clipboard opens them, updates links, saves PDFs (in the same folders with the same names as opened documents), saves and closes the INDD files:

var settings = save_setting();

var list = get_list_from_cliboard();

while (list.length) {
    var doc = app.open(list.shift());
    update_links(doc);
    save_pdf(doc);
    doc.close(SaveOptions.YES);
}

restore_settings(settings);


// functions ------------------------------------------------------------------

function get_list_from_cliboard() {
    var temp_doc = app.documents.add();
    app.paste();
    try { var contents = temp_doc.pages[0].textFrames[0].parentStory.contents.split('\r') }
    catch(e) { var contents = [] }
    temp_doc.close(SaveOptions.NO);
    return contents;
}

function update_links(doc) {
    var links = doc.links.everyItem().getElements();
    while(links.length) {
        var link = links.pop();
        if (link.status == LinkStatus.LINK_OUT_OF_DATE) link.update();
    }
}

function save_pdf(doc) {
    var name = doc.name.replace(/\.indd/g, '.pdf');
    var file = File(doc.fullName.parent + '/'  + name);
    var preset = app.pdfExportPresets.item("[High Quality Print]");
    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, file, false, preset);
}

function save_setting() {
    var settings = {
        checkLinksAtOpen: app.linkingPreferences.checkLinksAtOpen,
        viewPDF: app.pdfExportPreferences.viewPDF,
    };
    app.linkingPreferences.checkLinksAtOpen = false;
    app.pdfExportPreferences.viewPDF = false;
    return settings;
}

function restore_settings(settings) {
    app.linkingPreferences.checkLinksAtOpen = settings.checkLinksAtOpen;
    app.pdfExportPreferences.viewPDF = settings.viewPDF;
}

You can copy in Excel the list of paths to the INDD files run the script and get the PDF files and updated INDD files.

情绪少女 2025-02-14 10:01:11

基本上可以是这样的:

var doc = app.activeDocument;

// get the destination table 
// (a first table from the first text frame of the first page)
var table = doc.pages[0].textFrames[0].parentStory.tables[0];

// get the xls file
var xls_file = new File(doc.fullName.parent + '/table.xls');
// xls_file = xls_file.openDlg(); // <-- use it if you want the dialog window
if (!xls_file) exit();

// create a new text frame and put the contents of the xls file into it
var data_frame = doc.pages[0].textFrames.add();
data_frame.place(xls_file);

// convert the contents of the 'xls' frame into a 2D array 'data'
var data = [];
var data_text = data_frame.parentStory.tables[0].convertToText();
var paragraphs = data_text.contents.split('\r');
while (paragraphs.length) data.push(paragraphs.shift().split('\t'));

// remove the 'xls' frame
data_frame.remove();

// fill the destination table with contents of the 2D array
var rows = Math.min(table.rows.length, data.length);
var cols = Math.min(table.rows[0].cells.length, data[0].length);
for (var r=0; r<rows; r++) for (var c=0; c<cols; c++) {
    table.rows[r].cells[c].contents = data[r][c];
}

输入(Excel中的源表和Indesign中的目标表):

“

output(Indesign中的表):

Basically it can be something like this:

var doc = app.activeDocument;

// get the destination table 
// (a first table from the first text frame of the first page)
var table = doc.pages[0].textFrames[0].parentStory.tables[0];

// get the xls file
var xls_file = new File(doc.fullName.parent + '/table.xls');
// xls_file = xls_file.openDlg(); // <-- use it if you want the dialog window
if (!xls_file) exit();

// create a new text frame and put the contents of the xls file into it
var data_frame = doc.pages[0].textFrames.add();
data_frame.place(xls_file);

// convert the contents of the 'xls' frame into a 2D array 'data'
var data = [];
var data_text = data_frame.parentStory.tables[0].convertToText();
var paragraphs = data_text.contents.split('\r');
while (paragraphs.length) data.push(paragraphs.shift().split('\t'));

// remove the 'xls' frame
data_frame.remove();

// fill the destination table with contents of the 2D array
var rows = Math.min(table.rows.length, data.length);
var cols = Math.min(table.rows[0].cells.length, data[0].length);
for (var r=0; r<rows; r++) for (var c=0; c<cols; c++) {
    table.rows[r].cells[c].contents = data[r][c];
}

Input (the source table in Excel, and the destination table in InDesign):

enter image description here

Output (the table in InDesign):

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文