Skip to main content
April 7, 2011
Answered

Cut and Paste part of image from Photoshop to .csv template

  • April 7, 2011
  • 1 reply
  • 2904 views

Hi all,

I have my own javascript to determine which portion(coordinates) of an image i wish to cut.

But what codes can i add to my javascript to paste the image into an excel(.csv) template that i've prepared on the desktop?

And if the above is possible, how then to control which row/column in the excel(.csv) will the image be pasted in?

This is because the excel(.csv) is a Template, thus i will need the image to be pasted only in certain row/column...

Can someone help me?

Thanks so much in advance.

This topic has been closed for replies.
Correct answer Paul Riggott

Hi Paul,

Thanks for the code. It really made my work alot better.

May i ask, is it a MUST to have the (.xls) on the desktop?

i changed the code:

var Excel = File("/c/temp/pmr.xls");

to :

var Excel = File("~/desktop/myfolder/records/"+Number(paper_no)+".xls");

and i got the following error code:
Script:     C:\Users\Ando\AppData\Local\Temp\VBS.vbs
Line:       3
Char:     1
Error:     Object required: 'objWorkbook'
Code:     800A01A8
Source:     Microsoft VBScript runtime error
Would you know what caused this?


Very strange, I created the relevant folders and it all seems to work ok here, so don't know what's gone wrong?


#target photoshop
function main(){
if(!documents.length) return;
var paper_no=900;
var Excel = File("~/desktop/myfolder/records/"+Number(paper_no)+".xls");
var Cell ="C22";
if(!Excel.exists){
    alert("SpreadSheet does not exist!");
    return;
    }
try{
var SB = app.activeDocument.selection.bounds;
}catch(e){return;}
app.activeDocument.selection.copy();
var Width = SB[2].as('px') - SB[0].as('px');
var Height = SB[3].as('px') - SB[1].as('px');
var doc = app.documents.add(UnitValue(Width,'px'),UnitValue(Height,'px'),72);
app.activeDocument.paste();
doc.flatten();
var tmpFile = File(Folder.temp +"/deleteMe.jpg");
if(tmpFile.exists) tmpFile.remove();
SaveForWeb(tmpFile,60);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
var VBS = File(Folder.temp +"/VBS.vbs");
VBS.open('w');
VBS.writeln('Set objExcel = CreateObject("Excel.Application")');
VBS.writeln('Set objWorkbook = objExcel.Workbooks.Open("'+decodeURI(Excel.fsName)+'")');
VBS.writeln('objExcel.Visible = false');
VBS.writeln('Set mypic = objWorkbook.ActiveSheet.Pictures.Insert("'+decodeURI(tmpFile.fsName)+'")');
VBS.writeln('mypic.Top = objWorkbook.ActiveSheet.Range("'+Cell+'").Top');
VBS.writeln('mypic.Left = objWorkbook.ActiveSheet.Range("'+Cell+'").Left');
VBS.writeln('mypic.Width = objWorkbook.ActiveSheet.Range("'+Cell+'").Width');
VBS.writeln('mypic.Height = objWorkbook.ActiveSheet.Range("'+Cell+'").Height');
VBS.writeln('objExcel.ActiveWorkBook.Close True');
VBS.close();
VBS.execute();
}
main();

function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
   sfwOptions.format = SaveDocumentType.JPEG;
   sfwOptions.includeProfile = false;
   sfwOptions.interlaced = 0;
   sfwOptions.optimized = true;
   sfwOptions.quality = jpegQuality;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}

1 reply

Paul Riggott
Inspiring
April 7, 2011

You can't put a graphic in a csv file. You need to use a workbook and use another language other than JavaScript. (VB, .NET etc)

April 7, 2011

Hi Paul,

Thanks for letting me know. I was still relentlessly trying to find a means to cut it out into excel(.csv)

However, you mentioned that graphic cannot be placed in a csv file, is there any other format file that i can use javascript, cut and paste into?

I've spent much time programming my javascript to recognize the area i wish to cut, thus is much reluctant to change to VB etc...

Hope you may have a solution for my predicament.

Thanks!

regards

Paul Riggott
Inspiring
April 7, 2011

One way of doing it is to write the selection out to a file then call another program from your JavaScript to place the file into the excel workbook.

You will not be able to place the file into an excel spreadsheet using JavaScript only.