Javascript 从google电子表格导入数据以与ExtendScript工具包一起使用
我正在使用adobe的ExtendScript工具包来自动创建一些photoshop图像。我想阅读作为csv(公开)发布的google电子表格,并使用其中包含的数据编辑psd文件(其中一些名为“num”的文本层被电子表格中某列的内容替换)。以下是电子表格: 这是我的代码:Javascript 从google电子表格导入数据以与ExtendScript工具包一起使用,javascript,csv,google-sheets,extendscript,Javascript,Csv,Google Sheets,Extendscript,我正在使用adobe的ExtendScript工具包来自动创建一些photoshop图像。我想阅读作为csv(公开)发布的google电子表格,并使用其中包含的数据编辑psd文件(其中一些名为“num”的文本层被电子表格中某列的内容替换)。以下是电子表格: 这是我的代码: #target photoshop **import google spreadsheet here as a csv, and parse** for (i = 1; i < num_of_rows_in_csv_
#target photoshop
**import google spreadsheet here as a csv, and parse**
for (i = 1; i < num_of_rows_in_csv_file; i++) {
var fileRef = new File('/file.psd')
var doc = app.open(fileRef)
var layer = doc.layers.getByName('num');
if (layer.kind == LayerKind.TEXT) layer.textItem.contents = content_of_column_A_at_row_i; //replace text by content of cell
var opts, file;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
opts.quality = 100;
pngFile = new File("/file_" + i + ".png");
app.activeDocument.exportDocument(pngFile, ExportType.SAVEFORWEB, opts);
}
#目标photoshop
**将google电子表格作为csv导入此处,并解析**
对于(i=1;i
如何在ExtendScript中的javascript中实现这一点?ExtendScript中的csv读取
此scipt检测data.csv位于index.js旁边。data.csv的内容是:
| num | name | w | f | h |
| :--: | :--: | :--: | :--: | :--: |
| 1 | A | 1 | 4 | 2 |
| 2 | B | 2 | 3 | 1 |
| 3 | C | 3 | 2 | 4 |
| 4 | D | 4 | 1 | 3 |
| 5 | E | 3 | 2 | 2 |
| 6 | F | 2 | 3 | 1 |
这是一种又快又脏的方法。对于您不知道其格式的数据,我建议使用类似的库。只需稍作调整,它就可以在ExtendScript中正常工作
/* global File $ */
/* eslint-disable new-cap */
// tested on osx
var file = File(File($.fileName).parent.fsName + '/data.csv'); // get the file
file.encoding = 'UTF8'; // set some encoding
file.lineFeed = 'Macintosh'; // set the linefeeds
file.open('r',undefined,undefined); // read the file
var content = file.read(); // get the text in it
file.close(); // close it again
var lines = content.split('\n');// split the lines (windows should be '\r')
var data = [];// will hold the data
var keys = lines[0].split(','); // get the heads
// loop the data
for(var i = 1; i < lines.length;i++){
var obj = {}; // temp object
var cells = lines[i].split(',');// get the cells
// assign them to the heads
obj[keys[0]] = cells[0];
obj[keys[1]] = cells[1];
obj[keys[2]] = cells[2];
obj[keys[3]] = cells[3];
obj[keys[4]] = cells[4];
data.push(obj); // add to data
}
$.writeln(data.toSource()); // show what we got
你的问题是什么?如何阅读csv?谢谢!代码的核心工作正常,但我还需要从共享的google电子表格下载(每次运行代码)csv。我可以简单地更改“var file=”以添加csv的url吗?
文件不接受url,您需要以不同的方式下载它。您可以使用CEP面板或photoshop生成器插件。或者甚至尝试调用像curl这样的系统命令来下载您的文件。
[({num:"1", name:"A", w:"1", f:"4", h:"2"}), ({num:"2", name:"B", w:"2", f:"3", h:"1"}), ({num:"3", name:"C", w:"3", f:"2", h:"4"}), ({num:"4", name:"D", w:"4", f:"1", h:"3"}), ({num:"5", name:"E", w:"3", f:"2", h:"2"}), ({num:"6", name:"F", w:"2", f:"3", h:"1"})]