I'm currently trying to export an array to an excel file with cell formatting.
I'm starting off with this code here:
https://github.com/SheetJS/js-xlsx/blob/master/tests/write.js
But the problem is that whenever I'm trying to export it (save the file as an xlsx file) this is the error that shows up in the console:
Uncaught TypeError: Cannot read property 'writeFileSync' of undefined xlsx.js:5182
writeSync xlsx.js:5182
writeFileSync xlsx.js:5173
process_xlsx Test.html:379
reader.onload Test.html:438
The last 2 lines are basically the part of the code which says
XLSX.writeFile(wb, 'sheetjs.xlsx');
I know wb is not undefined as if I try and do console.log of it, the excel spreadsheet shows up properly :|
Can someone help me with this? I'm also trying to have each cell have a different formatting (IE different color/bolded/filled/etc)
You base your code on a node.js test. The documentation states:
Writing Workbooks
For writing, the first step is to generate output data. The helper
functions write and writeFile will produce the data in various formats
suitable for dissemination. The second step is to actual share the
data with the end point. Assuming workbook is a workbook object:
nodejs write to file:
/* output format determined by filename */
XLSX.writeFile(workbook, 'out.xlsx');
/* at this point, out.xlsx is a file that you can distribute */
write to binary string (using FileSaver.js):
/* bookType can be 'xlsx' or 'xlsm' or 'xlsb' */
var wopts = { bookType:'xlsx', bookSST:false, type:'binary' };
var wbout = XLSX.write(workbook,wopts);
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
/* the saveAs call downloads a file on the local machine */
saveAs(new Blob([s2ab(wbout)],{type:""}), "test.xlsx")
So to sum up: You try to use node.js internal functions in the browser, which fails. If you try to follow the seconds approach ( XLSX.write() instead of XLSX.writeFile()), you should be fine.
Related
I'm struggling to input value from Scanner option in Java to a txt file. while I can smoothly read the data using try{} catch{}, I cannot write the data from a scanner to the txt file. I can easily write data to txt file using PrintWriter, but that's not my goal... According to the scenario of the assignment, I have to create the system to input values and store the data text file, which I'm struggling to do.
Please help me with this problem, and provide me a solution...
This is my first Java project. Thanks
Scanner sc = new Scanner(System.in);
String data = sc.nextLine(); //taking input from user
// Use try with resource to release system resources
try ( FileWriter myWriter = new FileWriter("filename.txt"); ) {
myWriter.write(data); //writing into file
}
As you say you have successfully read (and maybe also manipulated) the data. Lets assume you have it ready to be written out as a String data and you also have a String filename of the file's intended name.
You can then do the following:
// generate the File object
File f = Paths.get("./" + filename).toFile();
f.delete(); // remove previous existing file -- equivalent to overwrite
try(BufferedWriter wr = new BufferedWriter(new FileWriter(f))){
wr.append(data); // adding the data into write buffer
wr.flush(); // writing the data out to the file
wr.close(); // closing the buffered writer
} catch (Exception e) {
e.printStackTrace();
}
I want to read data from a particular cell of my excel sheet and then use that data in my cypress tests. The file name is 'qaautomation.xlsx' and sheet name is 'Input' and I want to read data from cell B2. I have written the following code to access the value
/// <reference types ="cypress" />
var xlsx = require ("xlsx");
var workbook= xlsx.readFile("qaautomation.xlsx");
var worksheet= workbook.Sheets["Input"];
var cellB2value= 'B2';
var cellB2=worksheet[cellB2value];
var cellB2_value=(cellB2.v);
Code written below is where the value would be used.
describe('Typing the address', function(){
it ('should open the link', function(){
cy.visit(cellB2_value) //here comes the value from cell B2
})})
When I run the above code I get the following error
The following error originated from your test code, not from Cypress.
> _fs.readFileSync is not a function
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.
Check your console for the stack trace or click this message to see where it originated from.
Is there any way to remove this error?
This is a problem with how the XLSX read function deals with webpack and the browser's security feature to not access the file system directly.
You'll need to read the file via an ajax call and then 'read' it into xlsx that way. There's a demo here:
https://github.com/SheetJS/sheetjs/tree/master/demos/xhr
fetch(url).then(function(res) {
/* get the data as a Blob */
if(!res.ok) throw new Error("fetch failed");
return res.arrayBuffer();
}).then(function(ab) {
/* parse the data when it is received */
var data = new Uint8Array(ab);
var workbook = XLSX.read(data, {type:"array"});
/* DO SOMETHING WITH workbook HERE */
});
Well.. if you are using a (relatively) newer version of cypress there is a dedicated API just for that: https://docs.cypress.io/api/commands/writefile#Examples
From the docs:
cy.writeFile('path/to/message.txt', 'Hello World')
cy.readFile('path/to/message.txt').then((text) => {
expect(text).to.equal('Hello World') // true
})
yay!
move the functionality causing that into cypress/support/ such as in commands.js
After browsing a file I have to check if a csv file contains a formula or not in typescript or javascript.
Please help with an npm module or a function, as I need to protect a file from CSV Injection before uploading a file. The below code is what I have tried so far, it is just giving me the string-like #VALUE!, #NAME!, etc. instead I want =B1+C1
var files = e.target.files;
var file = files[0];
if(file) {
var reader = new FileReader();
reader.readAsText(file);
reader.onload = (event: any) => {
var csv = event.target.result;
var string = <string>csv ;
if(string.replace(/\s+/, "").includes(',-') == true) {
this.service.openSnackBar('PLEASE UPLOAD A VALID FILE','');
} else if (string.includes("+") == true || string.includes("=") == true) {
this.service.openSnackBar('PLEASE UPLOAD A VALID FILE','');
e.target.value = '';
} else {
// if valid upload to server
}
}
}
I was surprised to hear that CSVs could have embedded functions - I always thought that CSVs were simply text files, and relatively "safe" from vulnerabilities.
This is from OWASP (the Open Web Application Security Project):
https://owasp.org/www-community/attacks/CSV_Injection
This attack is difficult to mitigate, and explicitly disallowed from
quite a few bug bounty programs. To remediate it, ensure that no cells
begin with any of the following characters:
Equals to (“=”)
Plus (“+”)
Minus (“-“)
At (“#”)
So that's probably a good place to start:
Read your .csv a line at a time and parse into columns (e.g. string.split(','))
Ensure no column begins with any of the four characters above: =, +, - or #
Also read this post: Is there a way to programmatically insert formulas into a csv file using vb.net?
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'm trying to use NetUtil.asyncCopy to append data from one file to the end of another file from a Firefox extension. I have based this code upon a number of examples at https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O, particularly the 'Copy a stream to a file' example. Given what it says on that page, my code below:
Creates nsIFile objects for the file to copy from and file to append to and initialises these objects with the correct paths.
Creates an output stream to the output file.
Runs the NetUtil.asyncCopy function to copy between the file (which, I believe, behaves as an nsIInputStream) and the output stream.
I run this code as append_text_from_file("~/CopyFrom.txt", "~/AppendTo.txt");, but nothing gets copied across. The Appending Text and After ostream dumps appear on the console, but not the Done or Error dumps.
Does anyone have any idea what I'm doing wrong here? I'm fairly new to both Firefox extensions and javascript (although I am a fairly experienced programmer) - so I may be doing something really silly. If my entire approach is wrong then please let me know - I would have thought that this approach would allow me to append a file easily, and asynchronously, but it may not be possible for some reason that I don't know about.
function append_text_from_file(from_filename, to_filename) {
var from_file = Components.classes["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
from_file.initWithPath(from_filename);
var to_file = Components.classes["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
to_file.initWithPath(to_filename);
dump("Appending text\n");
var ostream = FileUtils.openFileOutputStream(to_file, FileUtils.MODE_WRONLY | FileUtils.MODE_APPEND)
dump("After ostream\n");
NetUtil.asyncCopy(from_file, ostream, function(aResult) {
dump("Done\n");
if (!Components.isSuccessCode(aResult)) {
// an error occurred!
dump(aResult);
dump("Error!\n")
}
});
}
asyncCopy() requires an input stream not a file.
you can do this:
var fstream = Cc["#mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
fstream.init(from_file, 0x01, 4, null);
NetUtil.asyncCopy(fstream, ostream, function(aResult)....