docs.sheetjs.com/docz/static/extendscript/esidwrite.jsx
2023-04-16 03:21:15 -04:00

48 lines
1.3 KiB
JavaScript

#include "xlsx.extendscript.js";
function workbook_add_table(wb, table) {
/* Collect Data */
var data = [];
var cnt = table.rows.count();
for(var R = 0; R < cnt; ++R) {
var row = table.rows.item(R);
data[R] = [];
var ccnt = row.cells.count();
for(var C = 0; C < ccnt; ++C) {
var value = row.cells.item(C).contents;
data[R][C] = value;
}
}
if(data.length == 0) return;
/* Create Worksheet */
var ws = XLSX.utils.aoa_to_sheet(data);
/* Create new Workbook and add worksheet */
XLSX.utils.book_append_sheet(wb, ws);
}
function main_write() {
/* Show File Picker */
var thisFile = File.saveDialog("Select an output file", "*.xlsx;*.xls");
if(!thisFile) { alert("File not found!"); return; }
/* Create new Workbook */
var wb = XLSX.utils.book_new();
/* Find all tables and add them to workbook */
var tfcnt = app.activeDocument.textFrames.count();
for(var i = 0; i < tfcnt; ++i) {
var tf = app.activeDocument.textFrames.item(i);
var tcnt = tf.tables.count();
if(tcnt == 0) continue;
for(var j = 0; j < tcnt; ++j) workbook_add_table(wb, tf.tables.item(j));
}
/* Write file to disk */
XLSX.writeFile(wb, thisFile.absoluteURI);
alert("Created File " + thisFile.absoluteURI);
}
main_write();