Changing Excel data with JavaScript - javascript

I am working on a tool where I control a Excel sheet using JavaScript.
Here is a requirement that, I need to change the Excel cell data.
I was doing something like this:
1. Open Excel as:
excel = new ActiveXObject("Excel.Application");
excel.Visible = false;
excel.DisplayAlerts = false;
workBook = excel.WorkBooks.open("c:\\excel.xls");
workSheet = workBook.Worksheets("Work_sheet_1");
2. Make change as:
WorkSheet.Cells(10,20).value = "10";
console.log("Value stored in excel is: "+WorkSheet.Cells(10,20).value);
3. Close as:
excelWorkbook.save();
excelWorkbook.Close(true, "c:\\excel.xls").
workBook.Close(false);
excel.application.quit();
excel = null;
I run it in browser, and I get log as:
Value stored in excel is: 10
Now whenever I make any change in Excel through JavaScript, I can always see the correct log report, But in excel, this changes are either saved at a delayed of 2 - 3 min, or never saves any change. this seems a kind of weird. Over that, when try to open the excel sheet manually from folder, it gives Edit-mode Lock Pop-up.
Can anyone tell me where am I heading wrong?

Related

getDatecreated in Appps Script returning the time of creation an hour later

When I try to get the time of creation of a file in Google Drive with the built in function getDateCreated() in the apps script, it returns the time an hour after the creation.
My code is as follows
var folder = DriveApp.getFolderById(myFolderId);
var contents = folder.getFiles();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var var_file;
var var_name;
var var_link;
var var_owner;
var var_time;
while(contents.hasNext()){
var_file = contents.next();
var_name = var_file.getName();
var_link = var_file.getUrl();
var_owner = var_file.getOwner().getName();
var_time = var_file.getDateCreated();
sheet.appendRow([var_name,var_link,var_owner, var_time]);
}
If time of creation of a file is 25/06/2022 22:48:39
The output of my code returns 25/06/2022 23:48:39 which is an error.
Note: My time zone is UTC-5
I did some research on different built-in methods in Javascript and the .toLocaleTimeString() seems to work fine returning the actual time the file was created.
var_time = var_file.getDateCreated().toLocaleTimeString();
It's also quite important to check the appsscript.json file which can be accesed by going to Project Settings and toggling on the option Show the appsscripts.json file
The only reason could be the time zone setup of your script file. You can check out bi going to File > Project properties in the script editor.
Google developers docs
Also you can review this setting running this:
var timeZone = Session.getScriptTimeZone();
Logger.log(timeZone);

Writing to Excel spreadsheet with ASP.Net site using the Excel Javascript API

I have created an ASP.Net webapp using the Empty Template in Visual Studio 2017. The website has many familiar Web Controls such as Button (s), ImageButton, and Label (s). Users open a picture inside the ImageButton control and are able to click inside the control. The webapp calculates a value depending on where the user clicks in the ImageButton, and the values are displayed in the corresponding Label controls. The user is meant to write the values into an open Excel Spreadsheet (this is where the issue lies). For additional context, every action taken by the user is handled by a client-side javascript function-- with the exception of opening the picture. The opening action is handled by C# code belonging to the aspx page.
In the process of writing a similar Excel Web Add In, I found some very helpful code for writing to an Excel Spreadsheet using the Excel JavaScript API.
Here is that very code:
function HighlightCell() {
Excel.run(function (ctx) {
// Create a proxy object for the selected range and load its properties
var sourceRange = ctx.workbook.getSelectedRange().load("values, rowCount, columnCount");
var sheet = ctx.workbook.worksheets.getActiveWorksheet()
// Run the queued-up command, and return a promise to indicate task completion
return ctx.sync()
.then(function () {
var highestRow = 0;
var highestCol = 0;
var highestValue = sourceRange.values[0][0];
// Find the cell to highlight
for (var i = 0; i < sourceRange.rowCount; i++) {
for (var j = 0; j < sourceRange.columnCount; j++) {
if (!isNaN(sourceRange.values[i][j]) && sourceRange.values[i][j] > highestValue) {
highestRow = i;
highestCol = j;
highestValue = sourceRange.values[i][j];
}
}
}
cellToHighlight = sourceRange.getCell(highestRow, highestCol);
cellToHighlight.format.fill.color = "IndianRed";
cellToHighlight.values = 5;
})
.then(ctx.sync);
})
.catch(errorHandler);
}
The code works like a charm within the Excel Web Add In, but it hasn't worked so far within the ASP.Net webapp. From my understanding, it is because the code hasn't been able to retrieve the active workbook / worksheet. This could be because of the disconnect between the server and the client-side from what I know.
Is there any way to open an excel spreadsheet on the client-side with javascript or C#. Can I even use the above code in an ASP.Net webapp?
EDIT: more code
I opened the spreadsheet on the client side with this code:
function excload() {
var selectedFile = document.getElementById('imgupload').files[0];
document.getElementById("frame").src = window.URL.createObjectURL(selectedFile);
}
In this code, imgupload is an HTML file input and "frame" is an iframe element. I'm not sure why when I run it, instead of just opening the spreadsheet in the iframe, it opens the spreadsheet in a new instance of the Excel program on the computer.
Noticed something weird in writing code:
Calling the function HighlightCell--which writes to the cell refreshes the page, while none of the other javascript functions do. This happens even if I add a return false; line to the function and call it from button with _onclick ="HighlightCell(); return false;"
REDUX of "Noticed something weird in writing code":
Managed to call Highlight cell without refresh by using:
$(document).ready(function () {
$('#chosen').click(HighlightCell);
});
But still no writing takes place

Google Apps Script to Open copy of Google Sheet in new window

I am trying to boost my Google knowledge by getting into scripting, and I have researched high and low for a solution to my situation to no avail.
I have a Google Sheet that is serving as a template. When the original file is opened, I want to launch a dialog to enter a student ID#. This number will pass into a specified cell in the template, which results in some auto-populated cells in the sheet (using spreadsheet functions). Once the cells have been populated, I want to make a copy of the file with a specific naming structure and store it in a specified folder in Google Drive. Lastly, I want that new file to open in a new window for further editing.
So far, I can get the dialog box to pop up for the ID#, I can pass that number to the sheet, and I can make a copy of the template and rename it accordingly.
What I CANNOT do is get the new file to open automatically in a new tab. I also need the new file to not run the "onOpen" script.
Here is what I have so far for each of these endeavors.
The code below creates a dialog box to enter a Student ID and passes it to the sheet to a specified cell:
function BuildUI() {
var app = UiApp.createApplication();
app.setTitle('Make a Copy - Please enter the Student ID# below:');
var panel = app.createVerticalPanel();
var textBox = app.createTextBox();
textBox.setName('stuId').setId('StudentID');
var button = app.createButton('Submit');
panel.add(textBox);
panel.add(button);
var clickHandler = app.createServerClickHandler('responses');
button.addClickHandler(clickHandler);
clickHandler.addCallbackElement(panel);
app.add(panel);
var doc = SpreadsheetApp.getActive();
doc.show(app);
}
function responses(e){
var app = UiApp.getActiveApplication();
var textBoxValue = e.parameter.stuId;
var sheet = SpreadsheetApp.getActiveSheet();
var studentID = sheet.getRange('Y4').setValue(textBoxValue);
var dateToday = sheet.getRange('C5').setValue(new Date());
return app.close();
}
The above code works for what I need it to do. I only include it for reference (and suggestions for cleaning it up ;)
This next code block makes a copy of the file with the new values, renames it, and saves it to a designated folder in Google Drive. Again..this works for me, but I include it for reference and suggestions:
function makeCopy(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var firstName = sheet.getRange('O4').getDisplayValue();
var lastName = sheet.getRange('E4').getDisplayValue();
var date = sheet.getRange('C5').getDisplayValue();
var stuId = sheet.getRange('Y4');
var grade = sheet.getRange('AH4').getDisplayValue();
var fileName = (lastName+ ", " +firstName+ " - " +stuId+ " - Grade " +grade+ " - " +date);
var destFolder = DriveApp.getFolderById('folderID');
destFolder.setSharing(DriveApp.Access.DOMAIN_WITH_LINK, DriveApp.Permission.VIEW);
DriveApp.getFileById(sheet.getId()).makeCopy(fileName, destFolder);
}
Lastly, my onOpen function strings this all together (plus one other function) to run as soon as the template is opened:
function onOpen(){
clearCells(); //Clear all content left behind by last editor
BuildUI();
makeCopy();
}
Any help in getting this workflow dialed in is greatly appreciated (most of this I have accomplished already, I just include it for you understanding of my overall needs):
User Interface Dialog Box to capture Student ID (done, but could use suggestions for cleaning up)
Pass ID to cell Y4 and current date to cell C5 (done...just included for understanding of overall need)
Make a copy of the original file and rename it to "Last Name, First Name - StuID - Grade - Date and store in specified Folder in Google Drive (done...just included for understanding of overall need)
NEED -- Open newly created file in a new tab in the browser without running the attached scripts (ideally, the scripts will not be included in the copy if possible)
Bonus points for any help in recreating the UI using the HtmlService in Google scripts since the UI Service is deprecated and functionality may go away the moment I figure all this out.
Thanks in advance for any help on this tall order.
You could capture the google file object in the makeCopy() with
gFile = DriveApp.getFileById(sheet.getId()).makeCopy(fileName, destFolder);
return gFile;
then add this function to open a new sheet:
function openNewSheet(gFile) {
var spreadsheetId = gFile.getId();
var url = "https://docs.google.com/spreadsheets/d/"+spreadsheetId;
//SpreadsheetApp.getUi().alert('url is ' + url);
var html = "<script>window.open('" + url + "');google.script.host.close();</script>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, "Open Sheet");
}
Finally you can add openNewSheet() to the end of onOpen()
Some credit goes to this video: https://www.youtube.com/watch?v=2y7Y5hwmPc4

How to make excel cells readonly using Javascript?

I have used ignite ui excel library to create an excel workbook using JavaScript. But unfortunately I didn't find any method to make columns/rows of excel read-only in their library. Is there a way we could make columns read-only before creating an excel sheet in JavaScript/Jquery?
I achieved this with the following code/steps:
By first making the entire excel sheet protected by using the code:
sheet.protect();
{sheet is my worksheet name}
Then by unlocking certain cells of excel sheet using the code:
sheet.getCell('H'+j).cellFormat().locked(false);
{where H is the column name and j is a row number, an integer value}
Hope that helps someone else.
overview:
Im using nodejs and exceljs and I was searching for save new row data on my xlsx file while the file is open for read the info (no to save) on windows 10, but due to excel lock the file i was not able to write to the file, exceljs threw me an exception ( Error : EBUSY: resource busy or locked). i was searching for the property "ReadOnlyRecommended" on exceljs for save the file with ReadOnlyRecommended = true, this way i can read the file and at the same time write on it (in the original file, because it is read only), but unfortunately exceljs doesnt have such option. So after a long search I achieved this using fs.chmod from
const fs = require('fs'); when i create for the first time or edit i use fs.chmodSync(excelFilePath, 0o600); for be able to write on the file but when i finish to write i use fs.chmodSync(excelFilePath, 0o400); to set the file on read only, this way when an user open the excel file this is in read only mode so excel will not lock the file.
i hope this help somebody.
https://www.geeksforgeeks.org/node-js-fs-chmod-method/
Excel.run(function (ctx) {
//Worksheet
var sheet = ctx.workbook.worksheets.getItem("Sheet1");
//Entire Range
var entireRange = sheet.getRange();
entireRange.format.protection.locked = false;
//Specific Range
var range = sheet.getRange("A1:B5");
return ctx.sync()
.then(() => {
//Set specific range "locked" status to true.
range.format.protection.locked = true;
})
.then(ctx.sync)
.then(() => {
//Protect Entire sheet
sheet.protection.protect({
allowInsertRows: false,
allowDeleteRows: false
});
});
}).catch(errorHandler);

I need to overwrite an existing Google Sheets file with an attached Script

I have a Google Sheets file with an attached Script. The script does a number of things, one is it makes a clone of it self using makeCopy. This portion works. Now I want to be able to keep the same cloned Google file name and same Google file ID and just update the content which includes a Spreadsheet and the associated Google script.
if (!fileFound){
var file = masterSSFile.makeCopy(reportFileName, RepFolder);
} else {
oldFile.setContent(masterSSFile.getBlob());
}
When I use makeCopy with the same file name it creates a second file with the same name but with a different file ID.
The else portion fails because .setContent argument seems to just accept text. The result is the word "Blob" in the oldFile, everything else is gone.
I have other scripts that update the contents of a existing spreadsheet by overriding the contents of the various sheets, but I also want the associated script to also be included in the updated file keeping the same file ID.
I found this....
Overwrite an Image File with Google Apps Script
and tried using
var masterSpreadsheetID = SpreadsheetApp.getActiveSpreadsheet().getId();
var masterSpreadsheetFile = DriveApp.getFileById(masterSpreadsheetID);
var oldFileID = oldFile.getId();
var oldFileName = oldFile.getName();
var newBlob = masterSpreadsheetFile.getBlob();
var file = {
title: oldFileName,
mimeType: 'application/vnd.google-apps.spreadsheet'
};
var f = Drive.Files.update(file, oldFileID, newBlob);
I get error: "We're sorry, a server error occurred. Please wait a bit and try again. " on this line: "Drive.Files.update(file, oldFileID, newBlob);"
After reading this:
https://github.com/google/google-api-nodejs-client/issues/495
it looks like Drive.Files.update(), does not support bound scripts.

Categories

Resources