I am looking to retrieve cells from an excel spreadsheet using JavaScript, to use the values in an HTML page. Currently, I do not know how to use SheetJS, and I am not able to use it to parse the worksheets.
What I am looking to do is: Retrieve Cell A1 -> Create a variable with the value of cell A1
Could anyone help me access the worksheet from JS and then get the value from an XLSX sheet? I have to use Excel for the purpose of this project.
Thanks!
function ReadData(cell, row) {
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("C:\\RS_Data\\MyFile.xls");
var excel_sheet = excel.Worksheets("Sheet1");
var data = excel_sheet.Cells(cell, row).Value;
document.getElementById('div1').innerText = data;
}
I had the same error.
You should try your path:
excel.Workbooks.Open("C://RS_Data//MyFile.xls");
// --> instead of --> \
Related
I have a large directory of workbooks that I need to change the parameters of a named range. I have recorded a Macro that can do this and I'm sure there's a quick way to apply?
I have a Tab in a directory workbook called 'Planner Fixes' & I have a list of Sheet ID's to send the amendment too.
The Macro is as below:
function ElementsEdit() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('API BootstrapStatic'), true);
spreadsheet.getRange('A2:BK514').activate();
spreadsheet.setNamedRange('Elements', spreadsheet.getRange('A2:BK1000'));
spreadsheet.getActiveSheet().hideSheet();
};
I know that the line var spreadsheet = SpreadsheetApp.getActive(); needs to change to look for the spreadsheet ID and 'open' that?
I'm very new to scripts and have generally only used ones I have found online but I can't seem to find anything to do this at all.
If you have multiple workbooks, and if you know the id of each of them, try
var spreadsheet = SpreadsheetApp.openById(YOUR_SPREADSHEET_ID);
for each
You can loop through a list of id
const ids = [
'1T7ixa-JGLgh8oxxxxxxxxxo4UzbivflUlwL9zLGVkgg',
'16uEeVNEBzBbGVTgKa98yyyyyyyyyyyy6mU-yKIZ0fKM',
'1d3ZHizzzzzzzzzzzzzzzzzzzLkmM4bFHyDn8RFtAlkk'
]
function myFunction() {
ids.forEach((id, i) => {
var spreadsheet = SpreadsheetApp.openById(id)
// your script here
})
}
I am trying to write a function where a specified workbook is opened and then we need to copy existing sheet and make a copy of it in the same workbook and rename that worksheet.
I saw this feature is implemented In VBA using the code below.
Worksheets("Sheet1").Copy After:=Worksheets("Sheet3")
In Javascript I am trying to do
var xlApp = new ActiveXObject("Excel.Application");
var xlWorkbooks = xlApp.Workbooks;
var xlWorkbook = xlWorkbooks.Open(xlFileName);
var xlWorksheets = xlWorkbook.Worksheets;
var xlWorkSheet = xlObj.getWorksheet("PRC1.1");
xlWorkSheet.copy();
This code is creating a copy in new workbook but I want in the same workbook.
Any Answers would be highly appreciated.
Thanks in Advance.
Currently, I am working on converting an Excel based calculator to a calculator that can be used on the web. This particular calculator relies on information stored in a data table with several thousands of cells.
Is there a way I can use JavaScript to get the value of a specified cell from the Excel data table? If so, how would I go about starting this process?
I have been searching for a way to do this all day, and I can't find a solution that will work for me. I tried saving the Excel table as an html file so I could work with the data that way, but the auto-generated output was over 70,000 lines and impossible to work with.
If anyone knows a way to get the value of a specified cell in Excel using JavaScript, please let me know. Any help will be greatly appreciated, and thank you in advance!
You can convert your Excel table to an *.csv file!
You can read the *.csv file in Javascript with the following code (I copied it from this thread: How to read data From *.CSV file using javascript?):
$(document).ready(function() {
$.ajax({
type: "GET",
url: "data.txt",
dataType: "text",
success: function(data) {processData(data);}
});
});
function processData(allText) {
var record_num = 5; // or however many elements there are in each row
var allTextLines = allText.split(/\r\n|\n/);
var entries = allTextLines[0].split(',');
var lines = [];
var headings = entries.splice(0,record_num);
while (entries.length>0) {
var tarr = [];
for (var j=0; j<record_num; j++) {
tarr.push(headings[j]+":"+entries.shift());
}
lines.push(tarr);
}
// alert(lines);
}
I'm using SheetJS in order to parse Excel sheets however I run into the following error:
"Uncaught TypeError: jszip is not a function"
When executing the following code:
var url = "/test-files/test.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(e) {
var arraybuffer = oReq.response;
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for(var i = 0; i != data.length; i++) arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
var workbook = XLSX.read(bstr, {type: "binary"});
}
oReq.send();
The original code is located here: https://github.com/SheetJS/js-xlsx
Are there any suggestions for an easier implementation of parsing Excel files?
Posting as answer(solution provided in comments worked) in case this might help someone else in the future:
It looks like you're using the src/xlsx.js version of xlsx.js, which is dependent on other source files, like jszip.js.
To fix this, use the dist version of xlsx.js located in dist/xlsx.js
Here is another solution for people who have problem when trying to use Excel file in JavaScript.Instead of reading an Excel file with JavaScript, you could directly use JavaScript in Excel with the help of the Funfun Excel add-in. Basically, Funfun is a tool that allows you to use JavaScript in Excel, therefore you don't need to use write additional code to parse Excel files.
Basically, what you need to do is
1). Insert the Funfun add-in from Office Add-ins store
2). Create a new Funfun or load a sample from Funfun online editor
3). Write JavaScrip code as you do in any other JavaScript editor. In this step, in order to directly use the data from the spreadsheet, you need to write some JSON I/O to make Excel cell reference. The place this value is in Setting-short but this would be just several lines. For example, let's assume we have some data like below in the spreadsheet.
In this case, the JSON I/O value would be:
{
"data": "=A1:E9"
}
Then in the script.js file, you just need to use one single line of code to read this data.
var dataset = $internal.data;
The dataset would be an array, each item would be one row in the spreadsheet. You could check the Funfun documentation for more explanation.
4). Run the code to plot chart
Here is a sample chart that I made using JavaScript(HighChart.js) and Excel data on Funfun online editor. You could check it on the link below. You could also easily load it to your Excel as described in Step2.
https://www.funfun.io/1/edit/5a439b96b848f771fbcdedf0
Disclosure: I'm a developer from Funfun.
So I am using a SharePoint web part (HTML Form Web Part) that I've built to query an excel file that is hosted on our Sharepoint site. I wrote javascript to create an Excel ActiveX object, and I can search the worksheet just fine.
However, I need to have it search for an exact match (the whole cell) not just part. I know the code that I need, I just can't make it work. I need to find out how to create the correct object type for the "xlWhole" argument of the "Find()" function.
The line of code I am having trouble with is commented out, because that didn't work. Any insight? I've seen the Range.Find method, but I just can't make it work.
var excel = new ActiveXObject("Excel.Application");
var wb = excel.Workbooks.Open("workbook path");
var ws = wb.Worksheets("worksheet name");
var ws = wb.ActiveSheet;
//var cell = ws.Cells.Find(str,excel.XlLookAt.xlWhole);
var cell = ws.Cells.Find(str);
foundRow = cell.Row;
This worked for me:
function searchExcel()
{
var excel =new ActiveXObject("Excel.Application");
excel.visible=true;
var wb = excel.workbooks.open("D:\\Analysis\\tmp\\Book1.xlsx");
var ws = wb.sheets(1);
var str="Value1";
// .Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection,
// MatchCase, MatchByte, SearchFormat)
var cell = ws.Cells.Find(str,ws.Cells(1),-4163,1)
alert(cell?cell.Row:(str+" not found"));
excel.quit();
}
I guess you cannot skip over parameters when using COM from js (but you can leave them off the end).