Read data from excel to database in sailsjs - javascript

Recently, I try to learn sails.js. Thus I learn it through google and internet, and sails.js is still new, I encountered some trouble while finding any example of reading data from excel (*.xls and *.xlsx files). Can anyone show me how? On google, I found xlsx module (install by npm install xlsx ) but I still dont know what I have to write in controller and in view files. Thank you a lot!

xlsx will permit you to read a file at the server level, but you're going to have to work on the code to get that data into your views. I can't tell if you're trying to upload files and then read them or just read a static file. At any rate I hope that this is a start as it surely isn't a comprehensive solution.
Here is some example code from one of my scripts that goes down the rows and gets some info from some cells.
var XLSX = require('xlsx'),
xls_utils = XLSX.utils;
var workbook = XLSX.readFile('./file.xls');
var num_rows = xls_utils.decode_range(sheet['!ref']).e.r;
var sheet = workbook.Sheets[workbook.SheetNames[0]];
for(var i = 0, l = num_rows; i < l; i++){
// Get cell in {c}olumn 2 (0=1 like arrays) and {r}ow: i
var cell = xls_utils.encode_cell({c:1, r:i});
var value = sheet[cell];
// Do something with value here
}
You can do a lot more with it and you'll have to play around with it. I find the documentation to be a little rough to get through but the info is all there.

Related

cannot find method copyTo error after creating new file in sheet

Would really appreciate your help here.
I'm creating a two step process where:
1. Create a new file named after a cell from the file to be copied from.
2. Copy the sheet to the newly created file.
I'm doing this because we will be creating unique files for several clients and I'd like to automate it.
Since I'm still new, I've been putting together code from various sources and modifying it to put what I want. However, this code creates a new file perfectly, but refuses to copy the content to the newly created file. I keep on getting a "cannot find method CopyTo error" and despite a ton of research and tweaking, I cannot get it to work. Help!?
function copy2() {
var folder=DriveApp.getFoldersByName("Dummy").next();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0,1];
var cellWithFileName = ss.getRange("A1");
var name = cellWithFileName.getValue();
var file=SpreadsheetApp.create(name);
var fileID = file.getId()
var copyFile=DriveApp.getFileById(fileID);
var destination = DriveApp.getFileById(fileID)
folder.addFile(copyFile);
DriveApp.getRootFolder().removeFile(copyFile);
ss.getSheetByName("Sheet1").copyTo(fileID);
}
Thanks
At copyTo(destination), destination is not file ID. It's Spreadsheet. So how about the following modification?
From :
ss.getSheetByName("Sheet1").copyTo(fileID);
To :
ss.getSheetByName("sheet1").copyTo(file);
Reference :
copyTo(spreadsheet)
If this didn't work, please tell me. I would like to modify.

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);

Cut and Paste audio using web audio api and wavesurfer.js

I am currently trying to make a web editor allowing users to easily adjust basic settings to their audio files, as a plugin I've integrated wavesurfer.js as it has a very neat and cross-browser solution for it's waveform.
After indexing a must-have list for the functionalities I've decided that the cut and paste are essential for making this product work, however after spending hours of trying to figure out how to implement this in the existing library and even starting to rebuild the wavesurfer.js functionalities from scratch to understand the logic I have yet to succeed.
My question would be if anyone can give me some pointers on how to start building a cut and paste functionality or maybe even an example that would be greatly appreciated.
Thanks in advance!
wavesurfer plugin:
http://wavesurfer-js.org
plucked web editor
http://plucked.de
EDIT Solution (instance is the wavesurfer object.):
function cut(instance){
var selection = instance.getSelection();
if(selection){
var original_buffer = instance.backend.buffer;
var new_buffer = instance.backend.ac.createBuffer(original_buffer.numberOfChannels, original_buffer.length, original_buffer.sampleRate);
var first_list_index = (selection.startPosition * original_buffer.sampleRate);
var second_list_index = (selection.endPosition * original_buffer.sampleRate);
var second_list_mem_alloc = (original_buffer.length - (selection.endPosition * original_buffer.sampleRate));
var new_list = new Float32Array( parseInt( first_list_index ));
var second_list = new Float32Array( parseInt( second_list_mem_alloc ));
var combined = new Float32Array( original_buffer.length );
original_buffer.copyFromChannel(new_list, 0);
original_buffer.copyFromChannel(second_list, 0, second_list_index)
combined.set(new_list)
combined.set(second_list, first_list_index)
new_buffer.copyToChannel(combined, 0);
instance.loadDecodedBuffer(new_buffer);
}else{
console.log('did not find selection')
}
}
Reading this answer suggests you can create an empty AudioBuffer of the size of the audio segment you want to copy (size = length in seconds ⨉ sample rate), then fill its channel data with the data from the segment.
So the code might be like this:
var originalBuffer = wavesurfer.backend.buffer;
var emptySegment = wavesurfer.backend.ac.createBuffer(
originalBuffer.numberOfChannels,
segmentDuration * originalBuffer.sampleRate,
originalBuffer.sampleRate
);
for (var i = 0; i < originalBuffer.numberOfChannels; i++) {
var chanData = originalBuffer.getChannelData(i);
var segmentChanData = emptySegment.getChannelData(i);
for (var j = 0, len = chanData.length; j < len; j++) {
segmentChanData[j] = chanData[j];
}
}
emptySegment; // Here you go!
// Not empty anymore, contains a copy of the segment!
Interesting question. First word that comes to mind is ffmpeg. I can't talk from experience but if I was trying to achieve this I would approach it like:
Let's assume you select a region of your audio track and you want to copy it and make a new track out of it (later maybe just appending it to an existing track).
Use the getSelection() method provided by the nice wavesurfer.js library. This will give you startPosition() and endPosition()[in seconds].
Given those points you now can use ffmpeg on the backend to select the region and save it as a new file (eventually upload it to S3,etc.). See this thread to get an idea of how to call ffmpeg from your ruby app (the commandline parameters shown there can be helpful too).
Note that if you plan to copy and paste many regions to piece up a new track, making this in the backend all the time will probably make no sense, and I guess I'd try to look for a client-side JS approach.
I hope this is helpful at least for an easy use case, and gets you started for the rest ;)
Update
This might be worth reading.
Web Audio API, tutorial here.
HTML5 audio - State of play. here(don't miss the section on TimeRanges, looks like a reasonable option to try).
This one(Outdated, but worth a look, interesting links).

Displaying .dcm files with XTK's X.volume()

According to the lesson 15 I pass .dcm file to the volume. I get no errors (e.g. no parsing errors I faced before) but nothing is displayed. What could be wrong with the .dcm files?
Here is a excerpt of the code I use:
function demo(file){
var _dicom = [file];
var r = new X.renderer3D();
r.init();
var v = new X.volume();
v.file = _dicom.map(function(v) {
return file;
});
r.add(v);
r.render();
r.onShowtime = function() {
v.volumeRendering = true;
};
};
I pass the full file path here. Well, I'm not sure it's a correct wording, but I'd lile to know, what dicom settings or parameters could cause such a behaviour, no errors and no diplayed data. Thanks in advance.
Did you try to drag the .dcm file into http://slicedrop.com and see the same effect? DICOM support currently only exists for uncompressed DICOM files and we didn't get Ultrasound too work yet. So on uncompressed MR data or CT data in DICOM it should work.

Javascript Download to excel sheet

There is an HTML table as
<table>
<tr><th>NAME</th></tr>
<tr><td>SAL</td></tr>
<tr><td>TOM</td></tr>
<tr><td>SAM</td></tr>
<tr><td>Jenny</td></tr>
</table>
Download to excel sheet
On click hyperlink how to save the table to excel sheet
You might want to try using the XLSX.js lib http://blog.innovatejs.com/?tag=xlsx-js
There is an example there on how to export to excel which gives examples.
Note this exports to the XLSX format not XLS. But this should not be a problem for most use. The source is on GitHub: https://github.com/stephen-hardy/xlsx.js
You may want to take a look at table2CSV, since Excel can open csv files with no problem (and as a bonus other software can do it too, like OpenOffice). If you need it to be cross-browser, there's no way of generating a downloadable file, for that you need a server side script like the one in the example in the page I linked.
I have developed a product called scriptscraper which is designed to solve that problem.
Get the trial installed because the demonstration projects download data from yahoo finance and store the data in Excel. For that simple html table it would be no problem to do what you need.
Try this:
function makeSheet() {
var x = theTable.rows
var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++){
var y = x[i].cells
for (j = 0; j < y.length; j++){
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}
}

Categories

Resources