forked from sheetjs/docs.sheetjs.com
32 lines
1.1 KiB
Plaintext
32 lines
1.1 KiB
Plaintext
|
const UXP = require("uxp");
|
||
|
const XLSX = require("./xlsx.full.min.js");
|
||
|
const storage = UXP.storage, ufs = storage.localFileSystem;
|
||
|
|
||
|
/* show file picker (single file, no folders) */
|
||
|
const file = await ufs.getFileForOpening({ types: ["xlsx", "xls", "xlsb"] });
|
||
|
/* read data into an ArrayBuffer */
|
||
|
const ab = await file.read({ format: storage.formats.binary });
|
||
|
/* parse with SheetJS */
|
||
|
const wb = XLSX.read(ab), wsname = wb.SheetNames[0];
|
||
|
const data = XLSX.utils.sheet_to_json(wb.Sheets[wsname], { header: 1, raw: false });
|
||
|
|
||
|
/* Set title */
|
||
|
app.activeDocument.textFrames.itemByName("Title").texts.item(0).contents = wsname;
|
||
|
|
||
|
/* Set table */
|
||
|
var tabeller = app.activeDocument.textFrames.itemByName("Table Frame");
|
||
|
var columns = data[0].length;
|
||
|
for(var R = 0; R < data.length; ++R) columns = Math.max(columns, data[R].length);
|
||
|
var table = tabeller.tables.add({
|
||
|
headerRowCount: 1,
|
||
|
bodyRowCount: data.length - 1,
|
||
|
columnCount: columns
|
||
|
});
|
||
|
for(R = 0; R < data.length; ++R) {
|
||
|
if(data[R] == null) continue;
|
||
|
for(var C = 0; C < data[R].length; ++C) {
|
||
|
if(data[R][C] == null) continue;
|
||
|
table.rows.item(R).cells.item(C).contents = data[R][C];
|
||
|
}
|
||
|
}
|