如何将 .xlsx 导入 InDesign

How to import .xlsx to InDesign

使用以下代码,我试图将 excel 文件导入 indesign。我可以获取数据,但如果 table 中的行数超过 3 行,它只会继续一行并且不会创建新行。

var doc = app.activeDocument;

// get a text from the XLSX file
var inputFile = File("~/Desktop/test.xlsx");
var temp_frame = doc.textFrames.add();
temp_frame.place(inputFile);
var text = temp_frame.parentStory.contents;
temp_frame.remove();

// make a table from the text
text = text.split('\r');

var table = [];
for (var row = 1; row < text.length; row++) {
    table.push(text[row].split('\t'));
}

// loop through the table and make the cards
for (var i = 0; i < table.length; i++) {

    var title       = table[i][0];
    var description = table[i][1];
    var price       = table[i][2];

    var card = make_card(title, description, price);

    // move card to some place
    card.move([10,10]);
    card.move(undefined, [i*75, 0]); 


     if(i > 2){
        card.move([20,10]);
        card.move(undefined, [i*75, 20]);
    } 
}



// the function to create and return a card
function make_card(title, description, price) {
    var doc = app.activeDocument;

    var title_frame = doc.textFrames.add();
    title_frame.geometricBounds = [20, 80, 30, 150];
    title_frame.contents = title;

    var description_frame = doc.textFrames.add();
    description_frame.geometricBounds = [30, 80, 80, 150];
    description_frame.contents = description;

    var price_frame = doc.textFrames.add();
    price_frame.geometricBounds = [80, 80, 100, 150];
    price_frame.contents = price;

    var group = doc.groups.add([title_frame, description_frame, price_frame]);
    return group;
}

我的问题是如何编写代码使卡片不离开文档但在下一行继续,以及如何在当前文档已满时自动添加新页面。

如果索引高于 2,我尝试在循环中使用以下代码执行此操作。将卡片向下移动。它向下移动但它在同一行中(在文档之外),我还认为有更好的方法来编写它,因为如果 excel 有 1000 个 rors,我必须设置这个 if 索引条件每三个元素......代码将是一个混搭

     if(i > 2){
        card.move([20,10]);
        card.move(undefined, [i*75, 20]);
    } 

对于这种情况,最简单直接的实现可能是这样的:

var doc = app.activeDocument;

// get a text from the XLSX file
var inputFile = File("~/Desktop/test.xlsx");
var temp_frame = doc.textFrames.add();
temp_frame.place(inputFile);
var text = temp_frame.parentStory.contents;
temp_frame.remove();

// make a table from the text
var rows = text.split('\r');
var table = [];
for (var i = 1; i < rows.length; i++) table.push(rows[i].split('\t'));

// width and height of cards, gaps, properties of the grid
var card_width = 70;
var card_height = 80;
var gap = 5;
var cols = 2;
var rows = 3;
var cards_per_page = cols * rows;

// calculate and add pages
var cards = table.length;
var pages_to_add = Math.ceil(cards / cards_per_page) - 1;
while(pages_to_add--) doc.pages.add();

var page_num = 0; // start page
var start_point = [10,10]; // start point for a first card on a page

main_loop:
for (var i = 0; i < cards; i++) {

    for (var row = 0; row < rows; row++) {

        for (var col = 0; col < cols; col++) {

            // break the loop if there is no more rows in the table
            if (table.length == 0) break main_loop;

            var table_row   = table.shift(); // take a first row from the table
            var title       = table_row[0];
            var description = table_row[1];
            var price       = table_row[2];

            // make a card
            var card = make_card(title, description, price);

            // send the card to the some page and move at some place
            card.move(doc.pages[page_num]);
            card.move(start_point);
            card.move(undefined, [(card_width + gap)*col, (card_height + gap)*row]);
        }
    }

    if (i > (page_num-1) * cards_per_page) page_num++; // increase the page number

}


// the function to create and return a card
function make_card(title, description, price) {
    var doc = app.activeDocument;

    var title_frame = doc.textFrames.add();
    title_frame.geometricBounds = [20, 80, 30, 150];
    title_frame.contents = title;

    var description_frame = doc.textFrames.add();
    description_frame.geometricBounds = [30, 80, 80, 150];
    description_frame.contents = description;

    var price_frame = doc.textFrames.add();
    price_frame.geometricBounds = [80, 80, 100, 150];
    price_frame.contents = price;

    var group = doc.groups.add([title_frame, description_frame, price_frame]);
    return group;
}