excel file while reading it through javascript - javascript

I know this types of question doesn't belong to here but this is my last hope to get answer and solution for this. I had made a html and javascript program to read excel file through Active X, but problem is that whenever I run that page the it runs fine but when i closed it the excel file remain open and if I run this page 100 times and then close it then there will be 100 unclosed excel file which leads to slowdown my PC. And second problem is that when I try to open that excel file which is being used by that application(the application is not currently running when i tried to open excel file) it does not open without giving an error,an window of excel file open and closed with in a small part of second..I don't know what to do to resolve this. If there is any problem in my code then here it is
var xVal = 1;
var yVal = 2
function readdata(x,y) {
x = xVal;
y = yVal;
try {
var excel = new ActiveXObject("Excel.Application");
excel.Visible = false;
var excel_file = excel.Workbooks.Open("D:\\Test1.xls");// alert(excel_file.worksheets.count);
var excel_sheet = excel_file.Worksheets("Sheet1");
var data = excel_sheet.Cells(x, y).Value;
//alert(data);
drawWithexcelValue(data);
xVal = xVal + 1;
}
catch (ex) {
alert(ex);
}
}
this is the code through which I am reading the excel file.

Try
data = null;
excel_sheet = null;
excel_file.Close(); // important
excel_file = null;
excel.Quit();
excel = null;
in this (reverse) order. This is (almost) equivalent to setting these values to Nothing in VBA.

Related

Automating Photoshop with JS

I had to place videos(mp4-files) in one photoshop document. I thought it would be easier to find a solution with png/jpg, and then project it on mp4. but the fact is that photoshop saving png/jpg and mp4 in different ways. Therefore, despite the fact that there is an import solution, I have difficulties with exporting mp4 by code.
I have 2 arrays of mp4 files and each mp4 from the first array needs to be overlaid on each of the second and saved by mp4. I solved the problem by uploading a video to an open photoshop file with a simple code:
function replaceContents(newFile) {
var docRef = app.open(newFile);
return docRef;
}
function importVideos(order_number) {
var doc = app.activeDocument;
var file = new File('E:/path/' + order_number + '.mp4');
// open a new document with needed video
var docTemp = replaceContents(file);
// copy opend layer with video from new doc to my main doc
var layer = docTemp.activeLayer.duplicate(doc.layerSets.getByName(color), ElementPlacement.PLACEATEND);
// close new unnecessary doc
docTemp.close(SaveOptions.DONOTSAVECHANGES);
layer.name = order_number;
return layer;
}
Here is the code for saving videos and in doExport() doc should be saved as a video.
function Saving(color) {
var array1 = app.activeDocument.layerSets.getByName('s');
var array2 = app.activeDocument.layerSets.getByName(color);
for (i = 0; i < 5; i++) {
array1.artLayers[i].visible = true;
for (j = 0; j < 5; j++) {
array2.artLayers[i].visible = true;
doExport();
array2.artLayers[i].visible = false;
}
array1.artLayers[i].visible = false;
}
}
So a new question: how to export a video from photoshop with a code with the ability to specify the file name and the save path?
P.S. if you do this through Actions, you can't enter input parameters like the name of the saved file, it seals the Action as you did it.
If you know how to create arguments for Actions, you are welcome!

Opening an excel file using Protractor

Protractor Query: our application generate a excel file which contains aome formulas. Now until I don't open the excel, it won't readable by exceljs. Is there any way I can open the excel and close it like we do robot in selenium?
Please Note that if i Open the file and keep at specified location below code works fine, but when i download the same from application and try to read it it wont work.
function compareExcelFromScreen(VerificationSection, file, row, column, SheetName, callback) {
var wrkbook = new Excel.Workbook();
var valFromExcel = null;
var filteredExcelValue = null;
var filteredScreenvalue = null;
var temp = null;
wrkbook.xlsx.readFile(file.toString()).then(function () {
var worksheet = wrkbook.getWorksheet(SheetName);
valFromExcel = worksheet.getRow(parseInt(row)).getCell(parseInt(column)).value;
}
}

Execution Timeout for Google Apps Script

I'm currently writing a script that copies a folder, and all of its sub-folders and files of a very large directory from one location in Google Drive to another. This is being used for archiving purposes for the company which I am employed by.
My issue is that the size and quantity of the file that I am trying to archive is WAY too large to handle in the 5 minute execution time given by Google. I am trying to keep this script as a standalone web app, however I am happy to extend on it as needed.
My second issue is that I need the script to run over and over again until the folders have been copied but once it is finished I need it to stop. I was originally going to use triggers, however time-based triggers are inappropriate for the task I am trying to fulfil.
In short, I need my script to run until the task is completed, automatically restarting itself, and avoiding execution time errors.
The full code is included below.
//Global Variables
var classroomFolderName = "Classroom";
var archiveFolderName = "Archive";
function doGet(){
var classroomFolder = DriveApp.getFoldersByName(classroomFolderName).next();
var archiveFolder = DriveApp.getFoldersByName(archiveFolderName).next();
start(archiveFolder);
}
function getFolder(folderName){
var foldercount = 0;
//Selects all folders named exactally as parameter is given
var folder = DriveApp.getFoldersByName(folderName);
while (folder.hasNext()) {
var folders = folder.next();
foldercount++;
}
//Throws errors if number of folders != 1
if (foldercount < 1){
throw 1;
}
else if (foldercount > 1){
throw 2;
}
else{
return folder;
}
}
function start(archiveFolder) {
var sourceFolder = classroomFolderName;
var targetFolder = "New Archive";
var source = DriveApp.getFoldersByName(sourceFolder);
var target = archiveFolder.createFolder(targetFolder);
if (source.hasNext()) {
copyFolder(source.next(), target);
}
}
function copyFolder(source, target) {
var folders = source.getFolders();
var files = source.getFiles();
while(files.hasNext()) {
var file = files.next();
file.makeCopy(file.getName(), target);
}
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
var targetFolder = target.createFolder(folderName);
copyFolder(subFolder, targetFolder);
}
}
This is something I came across- you may find some value in it-
https://ctrlq.org/code/20016-maximum-execution-time-limit

Importing an excel (xls) file into javascript

I would like to import an excel file into javascript.
I already have got a code that works (kind of).
This is my code:
function GetData(cell,row){
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("U:\\File1.xls");
var excel_sheet = excel.Worksheets("Sheet1");
var data = excel_sheet.Cells(cell,row).Value;
return data;
}
The code only works in IE (because of ActiveX), and it is incredibly slow.
Another problem is that it can only get the data from the first 100 rows or so, after that it just loads for hours and I get an ObjectError.
Another way to import excel files, or something that works the same way, would be greatly appreciated.
Updated Code:
Opens the file just once. But now i get an Object Error inside of the try-catch, when i try to run the GetData(i,2) method.
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("U:\\Datei.xls");
var excel_sheet = excel.Worksheets("Sheet 1");
function GetData(cell,row){
var data = excel_sheet.Cells(cell,row).Value;
return data;
}
function main(){
try{
for(var i=1; i<5000;i++){
if(text1 == GetData(i,2)){
alert(GetData(i,6));
throw "Entry found";
}
}
}
catch(e) {
alert(e)
}
}

Exporting table to Excel only works from development machine, not from server

I am using the following piece of code to export tables to Excel but I just realized if I upload this to a server, it will not work. It will only work on the development machine.
function writeToExcel(tableID,columnas)
{
str="";
var myTableHead = document.getElementById(tableID);
var rowCount = myTableHead.rows.length;
var ExcelApp = new ActiveXObject("Excel.Application");
var ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelSheet.Application.Visible = true;
var celda = new Array(columnas);
//f = Nro Filas
getN(tableID);
for(f=0;f<n;f++){
//g = Nro Columnas
for(g=0;g<columnas;g++)
{
//if(g!=1 || g!= 8 || g!=10 || g!= 12){
str= document.getElementById(tableID+f+""+g).innerHTML;
ExcelSheet.ActiveSheet.Cells(f+1,g+1).value = str;
}
}
}
am I missing something?? In the dev machine it works wonderfully! but on the server side it doesn't. Is there a better way to do this? Maybe using jQuery?
I get an error on the server side saying:
Automation server can't create object and it gives me the error on the following line,
var ExcelApp = new ActiveXObject("Excel.Application");
Any help is greatly appreciated
Excel is not designed to run on the server. There are a number of ways to write out excel on the server using server side technology. PLease see http://support.microsoft.com/kb/257757
In your case you could write the same html table to the response stream and set the mime type to application/vnd.ms-excel

Categories