javascript - 使用 javascript 将差异与 CSV 中找到的数据连接起来
问题描述
我可以在 CSV 中搜索文件名,并检查它是否已经存在:
//Open created .csv and check if file is already there
folderName = "~/Desktop/"
docRef = activeDocument;
var now = new Date();
var logfile_name = now.getFullYear() + "-"+ now.getMonth() + "-" + now.getDate() + '.csv'
var fileOut = new File(folderName+logfile_name);
fileOut.open("r");
var str ="";
while(!fileOut.eof)
str += fileOut.readln();
fileOut.close();
//input what to search for
var n = str.match(docRef.name);
我总是可以添加一个新行并仍然保留标题:
folderName = "~/Desktop/"
//Create File with todays date
var now = new Date();
var logfile_name = now.getFullYear() + "-"+ now.getMonth() + "-" + now.getDate() + '.csv'
var fileOut = new File(folderName+logfile_name);
if (!fileOut.exists) {
fileOut.open("w");
fileOut.writeln("Filename, Department, Steps, Tools, Liquify, Pen Tool, Clone Stamp, Patch Tool, Spot Healing Brush, Free Transform, Dust and Scratches, Color Correction, Replace Color");
fileOut.writeln(docRef.name, ",", departmentNumber, ",", MyHistoryStates, ",", HowManyTools, ",", liquifyUsed, ",", pentoolUsed, ",", clonestampUsed, ",", patchtoolUsed, ",", spothealingbrushUsed, ",", freetransformUsed, ",", dustandscratchesUsed, ",", colorcorrectionUsed, ",", replacecolorUsed);
} else {
fileOut.open("a");
fileOut.writeln(docRef.name, ",", departmentNumber, ",", MyHistoryStates, ",", HowManyTools, ",", liquifyUsed, ",", pentoolUsed, ",", clonestampUsed, ",", patchtoolUsed, ",", spothealingbrushUsed, ",", freetransformUsed, ",", dustandscratchesUsed, ",", colorcorrectionUsed, ",", replacecolorUsed);
}
fileOut.close();
但我现在要做的是,这样下次我运行脚本时,它会检查:
- 如果文件名已经在 CSV 中,那么
concatenate
MyHistoryStates
(CSV 中的“步骤”)。 - 如果 9 个布尔值中的任何一个发生了变化,则将其替换为新值。
- 无需创建新的行/单元格,只需更新已有的内容
不确定这是否有意义:(
解决方案
我会做这样的事情:使用所有数据和文件名作为属性创建一个对象对象。如果新文件名已经在此对象中,请更新您需要的步骤和内容,如果它不在对象中,请将其添加为新项目。然后将对象转换回字符串并写入 csv 文件。
//fileOut is defined already
fileOut.open("r");
var myObj = {};
while (!fileOut.eof)
{
var csvLine = fileOut.readln().split(','); //create an array from csv string
myObj[csvLine[0]] = createObject(csvLine); //create an object {'S208032.tif':{department: 32, steps: 5, tools: false, etc}}
};
//newCsvFile is defined already and is an array, similar to fileOut.readln().split(',')
myNewData[newCsvFile[0]] = createObject(newCsvFile)
for (var fileName in myObj)
{
// if filename from newCsvFile is already in the csv
if (fileName == newCsvFile[0])
{
//update steps
myObj[fileName].steps += myNewData[fileName].steps;
// change other properties as you want
myObj[fileName].liquify = myNewData[fileName].liquify
// etc...
}
// if filename from newCsvFile isn't in the csv
else
{
//create a new entry in myObj with data from the new string
myObj[newCsvFile[0]] = myNewData[newCsvFile[0]];
}
};
//transform objects to strings
//write csv file
function createObject(data)
{
return {
department: data[1],
steps: data[2],
tools: data[3],
liquify: data[4],
//etc...
}
};
推荐阅读
- arrays - 根据其他相关数组确定一个数组的元素位置
- jax-rs - 在 Quarkus 本机中运行的 JAX-RS 应用程序中使用 Jedis 池会导致 ClassNotFoundException:org.apache.commons.pool2.impl.DefaultEvictionPolicy
- html - 我在哪里可以找到这个“历史”符号作为字符?
- javascript - 这样做的正确方法是什么?我得到未定义的结果,因为该变量尚未被赋值
- php - Laravel ErrorException(代码:0):stdClass类的对象无法转换为字符串
- oracle-apex - 交互式网格中列标题中的文本长度
- oracle - DBMS_RANDOM.VALUE 返回值等于 HIGH 值
- java - 您将如何使用 Spring 创建具有多个可选参数的 RESTful 服务
- java - 本地类不兼容:流 classdesc
- python - 数组中大数的索引