首页 > 解决方案 > 使用 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();

目前,运行 3 次后输出如下: 在此处输入图像描述

但我现在要做的是,这样下次我运行脚本时,它会检查:

所以它看起来像这样: 在此处输入图像描述

不确定这是否有意义:(

标签: javascriptphotoshop

解决方案


我会做这样的事情:使用所有数据和文件名作为属性创建一个对象对象。如果新文件名已经在此对象中,请更新您需要的步骤和内容,如果它不在对象中,请将其添加为新项目。然后将对象转换回字符串并写入 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...
    }
};

推荐阅读