Save data to a new Workbook with office.js - javascript

Using office.js, is there a way to create a new workbook and then edit the new workbook?
I thought about these two options but couldn't find a way to implement any of them:
(preferred): Get an object reference when creating the new workbook via Excel.createWorkbook(), similar to VBA, like
var newWorkbook = Excel.createWorkbook();
newWorkbook.doStuff();
Create the data in a new Worksheet within the source Workbook and then save this Worksheet as a new Workbook

Related

Copy worksheet to same workbook and rename it using Javascript

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.

Process a local CSV file with native JavaScript

I am new to this stuff.
I'm trying to build a website about finance.
I have written a scrypt in python to get shares data from an API, created my own market index and exported the datas of my index in a CSV file. It's important because, I need to create an historic and observe its evolution (the script will run automatically in a VM to add data as it goes). No problem at this stage.
Now, my purpose is to read and treat my CSV file in a JS script to stock the datas into an array (graphJS use array to display a graph and that's what I want to use).
I only found solutions that use an <input> HTML (user import a CSV on a website, I do not need that) or JSQuery (I have never been able to use it)
I'm looking for a native JavaScript solution. For the beginning, I am just looking to display datas from my CSV in console.log(). And with a "for" loop, use ".push" to iterate the datas into an array. But I will do this after I successfully displayed my file in the console.
My code does not work, can someone help me ?
const csvLocalFile = "file.csv";
const csv = csvLocalFile.files[0];
const reader = new FileReader();
reader.readAsText(csv);
TypeError: Cannot read properties of undefined (reading '0')
The Issue Is That You Are Trying To Access The File Contents From A Variable Of Type String.
What Your Code Does Is Try To Reference An Element Inside This String Like A Map Which doesn't Exist. Giving The Error.
Also In The Code It Only Creates A New Variable whose contents are file.csv and doesn't actually store the file.
To Actually Get The Contents Of Your CSV:
var filecontent
// https://stackoverflow.com/a/29176118
var openFile = function(event) {
var input = event.target;
var reader = new FileReader();
reader.onload = function(){
var text = reader.result;
filecontent = text.replace("","")
};
reader.readAsText(input.files[0]);
};
And In Your HTML
<input type="file"onchange="openFile(event)">
Your CSV Contents Will Be Stored Inside the Variable filecontent
EDIT: Non File Upload Types
let obj = await (await fetch('file.csv')).text();
This Contains Your CSV.

Retrieving Cell Values from an Excel Spreadsheet using JavaScript

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 --> \

Parsing Excel sheet using javascript

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.

Create a new blob in memory using Apps Script

I want to create a new blob in memory, put the content in the blob, name it, and finally save it as a file in Drive.
I know how to create a file in Drive from blobs. The only thing I'm looking for is creating the empty new blob to start with.
You can create a new blob in memory with the Utilities Service:
function createNewBlob() {
var blobNew = Utilities.newBlob("initial data");
blobNew.setName("BlobTest");
Logger.log(blobNew.getName());
blobNew.setDataFromString("Some new Content");
Logger.log(blobNew.getDataAsString())
var newFileReference = DriveApp.createFile(blobNew);
newFileReference.setName('New Blob Test');
};
You can also create a new file, with some small amount of data, then change the content. In this example, a file is created in the root directory with the contents "abc". Then the content of the blob is set to something else.
function createNewBlob() {
// Create an BLOB file with the content "abc"
var blobNew = DriveApp.createFile('BlobTest', 'abc', MimeType.Blob);
Logger.log(blobNew.getName());
blobNew.setContent("Some new Content");
Logger.log(blobNew.getBlob().getDataAsString())
};
You could create data in memory, then create the blob with the finished data.

Categories