13 Replies Latest reply on Apr 13, 2011 6:06 AM by andosg_Ryan

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

    andosg_Ryan Level 1

      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.

        • 1. Re: Cut and Paste part of image from Photoshop to .csv template
          Paul Riggott Level 6

          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)

          1 person found this helpful
          • 2. Re: Cut and Paste part of image from Photoshop to .csv template
            andosg_Ryan Level 1

            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

            • 3. Re: Cut and Paste part of image from Photoshop to .csv template
              Paul Riggott Level 6

              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.

              • 4. Re: Cut and Paste part of image from Photoshop to .csv template
                Paul Riggott Level 6

                Here is an example of saving a selection and inserting it into an existing Excel workbook.

                Windows only though.

                 

                #target photoshop
                function main(){
                if(!documents.length) return;
                var Excel = File("/c/temp/test.xls");
                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('objWorkbook.ActiveSheet.Pictures.Insert("'+decodeURI(tmpFile.fsName)+'")');
                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);
                }
                
                • 5. Re: Cut and Paste part of image from Photoshop to .csv template
                  andosg_Ryan Level 1

                  Hi Paul,

                   

                  Thanks so much!

                   

                  However, would you be able to advise me, if it is possible to paste onto the (.xls) file specifically into certain rows/columns?

                  ie. If i wish to paste in B14 in a excel workbook?

                   

                  i've tried to use it for a (.csv) format, but it doesnt work. Would you know why?

                   

                  Do hope you have a great day ahead!

                  • 6. Re: Cut and Paste part of image from Photoshop to .csv template
                    Paul Riggott Level 6

                    A csv file is text only so you can't put a graphic into it.

                    I have Excel 2007 but that doesn't let you put a graphic into a cell so I don't know if its possible.

                    • 7. Re: Cut and Paste part of image from Photoshop to .csv template
                      andosg_Ryan Level 1

                      Hi Paul,

                       

                      Noted that (.csv) cannot have graphics in it. Many thanks.

                       

                      About using (.xls) and pasting the image through VBS, will it be possible to control where the image lands on in the (.xls)?

                      Not neccessarily in any cells.

                       

                      Currently, once the script completes, i find the graphic pasted at a random position in the (.xls).

                       

                      Thanks so much for the help so far.

                      • 8. Re: Cut and Paste part of image from Photoshop to .csv template
                        Paul Riggott Level 6

                        Sorry but can't find a way of aligning a picture in Excel. I have tried making a cell active before adding the picture but that doesn't make any difference.

                        Maybe someone else might have an answer as I don't do much with Excel and VBS.

                        • 9. Re: Cut and Paste part of image from Photoshop to .csv template
                          Paul Riggott Level 6

                          Just found a way of aligning the top left of the image to a cell....

                           

                          #target photoshop
                          function main(){
                          if(!documents.length) return;
                          var Excel = File("/c/temp/pmr.xls");
                          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("B14").Top');
                          VBS.writeln('mypic.Left = objWorkbook.ActiveSheet.Range("B14").Left');
                          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);
                          }
                          
                          • 10. Re: Cut and Paste part of image from Photoshop to .csv template
                            Paul Riggott Level 6

                            If you want to make it appear that the image is in a cell....

                             

                            #target photoshop
                            function main(){
                            if(!documents.length) return;
                            var Excel = File("/c/temp/pmr.xls");
                            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("B14").Top');
                            VBS.writeln('mypic.Left = objWorkbook.ActiveSheet.Range("B14").Left');
                            VBS.writeln('mypic.Width = objWorkbook.ActiveSheet.Range("B14").Width');
                            VBS.writeln('mypic.Height = objWorkbook.ActiveSheet.Range("B14").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 person found this helpful
                            • 11. Re: Cut and Paste part of image from Photoshop to .csv template
                              andosg_Ryan Level 1

                              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?

                              • 12. Re: Cut and Paste part of image from Photoshop to .csv template
                                Paul Riggott Level 6

                                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);
                                }
                                
                                • 13. Re: Cut and Paste part of image from Photoshop to .csv template
                                  andosg_Ryan Level 1

                                  Hi Paul,

                                   

                                  I've tried it again. It works well.

                                   

                                  may i trouble you once more? I am literally handicapped in VBS.

                                   

                                  i wish to add the Current Date/Time in say B13 and C13 cell.

                                  How do i go about doing it?

                                   

                                  i've tried writing in the function (Date), but cant seem to work at all.