javascript internet explorer byte order mark - javascript

I am working on a way to save an HTML table to a csv file. Ideally, this should be cross-browser, and I have gotten this to work on everything but Internet Explorer. However, I have gotten the obvious parts working. What remains is that I am unable to get a working csv file from my JavaScript because a byte order mark is prepended to the data I wish to download.
I have confirmed that this is the case by downloading the csv file in IE and everything else and used a hex editor to view the raw file, I can confirm the file that Internet Explorer downloaded prepends the unicode character "FFFE".
Please see the code below this does this. saveTable takes an "<a>" node that is located inside a table.
If anyone can help me disgnose the issue and offer some solution I'd be grateful. Please forgive any faux pas on my part, I don't think I've ever used a site of this nature before. So if you need me to provide any further information please do just let me know and I shall do my best to get it on here.
function findTable(node) { // Finds a nodes parent table.
return (node.nodeName !== "TABLE") ? findTable(node.parentNode) : node;
}
function saveTable(node) {
var csv = [];
var table = findTable(node);
var rows = table.getElementsByTagName("tr");
var header = [];
var csv = [];
for (var i = 0; i < rows.length; i++) {
if (i == 0) {
// Do csv stuff.
var dates = rows[i].getElementsByTagName("th");
for (var j = 0; j < dates.length; j++)
(j == 0) ? header.push("") : header.push(dates[j].innerHTML);
csv.push(header.join(","));
}
else {
var rowArray = [];
var jobName = rows[i].getElementsByTagName("th")[0].innerHTML;
var times = rows[i].getElementsByTagName("td");
rowArray.push(jobName);
for (var k = 0; k < times.length; k++)
rowArray.push(times[k].innerHTML);
csv.push(rowArray.join(","));
}
}
node.setAttribute("href", "data:text/csv;charset=utf-8," + csv.join("%0A"));
var fileName = "spreadsheet_data-" + (new Date).getTime() + ".csv";
if (node.download == "")
node.setAttribute("download", fileName);
else {
alert("Handle IE here!");
var bom = "\uFFFE";
var doc = document.open("application/octet-stream", "_blank");
var data = csv.join("\r\n");
doc.charset = "UTF-8";
doc.write(data.replace(bom, ""));
doc.focus();
doc.execCommand('SaveAs', false, fileName);
doc.close();
}
}
Table example, it's not the way I would have chosen to do it myself, but it's how the table is generated by another piece of software.
<table id='results' border='1'>
<tr><th><a href='#' onClick='saveTable(this);' id='download_link'>Download data</a></th><th>2013/05/09</th><th>2013/05/10</th><th>2013/05/10</th><th>2013/05/10</th><th>2013/05/10</th></tr>
<tr>
<th>\PDF\EXOVIGN.PDF</th><td>8.853</td><td>9.050</td><td>8.807</td><td>8.827</td><td>8.835</td></tr>
</table>

If you have no absolute requirement to do this client-side, it might save you a lot of hassle to send the file from the server instead.

Related

Optimize for-loop function in Google Apps Script (Arrays maybe)?

First-time poster here. I would like some insight on some Google App Script code i think could be spruced up a bit.
Long story short.
I have a 2 Google Sheet tables
A “LOCALE” spreadsheet - listing unique names of locations
A “FEED” spreadsheet - listing photo descriptions, including the locations. The same location is listed multiple times in this spreadsheet.
Both of these tables have a “Location” column, which references each other with a Key column.
The problem I want to solve for:
When I edit a location name in the “LOCALE” spreadsheet, it should automatically update all the location names in the “FEED” spreadsheet.
The way I solved this problem:
I used a for loop within a for loop for this. To summarize:
for every row in "LOCALE"...
..go through every row in "FEED"...
...If a value in the Key column in the FEED Sheet matches a value in the Key column in the LOCALE Sheet...
...but the value in the Location column in the FEED Sheet doesn't match the value in the Location column in the LOCALE Sheet...
...update the Location column in the FEED Sheet with the value in the Location column in the LOCALE Sheet.
If you're not confused yet, here's the code i wrote for it:
// for each row in the "Locale" sheet...
for(var L = LocationsRefValues.length-1;L>=0;L--) {
// for each row in the "Feed" sheet...
for(var F = FeedRefValues.length-1;F>=0;F--) {
if (FeedRefValues[F][97] == LocationsRefValues[L][17] &&
FeedRefValues[F][10] != LocationsRefValues[L][1]) {
FeedDataSheet.getRange(F+2,10+1).setValue(LocationsRefValues[L][1]);
}
}
}
Now, this code works perfectly fine, I've had no issues. However, i feel like this a bit clunky, as it takes a while to finish its edits. I'm certain there's any easier way to write this and run this code. I've heard arrays may address this situation, but i don't know how to go about that. Hence, why I'm looking for help. Can anyone assist?
Keep in mind I'm a total Google App Script beginner who got this code working through sheer dumb luck, so the simpler the solution the better. Thanks for any consideration to my problem in advance. Looking forward to hearing from you all.
This is the full function (after i made edits suggested here.)
function ModeratorStatus() {
var Data = SpreadsheetApp.getActiveSpreadsheet(); // Local Spreadsheet
var ModeratorStatusDataSheet = Data.getSheetByName("The Status (Moderators)");
var ModeratorStatusRange = ModeratorStatusDataSheet.getRange("A2:C");
var ModeratorStatusRefValues = ModeratorStatusRange.getValues();
var ModeratorDataSheet = Data.getSheetByName("The Moderator_Numbers"); // DATA "Member" sheet
//var ModeratorRefValues = ModeratorDataSheet.getRange("A2:AD").getValues();
var ModeratorStatusObj = {};
for (var MOS = ModeratorStatusRefValues.length-1; MOS>=0; MOS--) {
ModeratorStatusObj[ModeratorStatusRefValues[MOS][2]] = ModeratorStatusRefValues[MOS][0];
}
var ModeratorValues = ModeratorDataSheet.getRange("A1:AD").getValues();
for (var MO = ModeratorValues.length-1; MO >=0; MO--) { // for each row in the "Moderator" sheet...
var ModeratorVal28 = ModeratorValues[MO][28];
if (ModeratorStatusObj[ModeratorVal28] != ModeratorValues[MO][1]) {
ModeratorValues[MO][1] = ModeratorStatusObj[ModeratorVal28];
}
}
var destinationRange = ModeratorDataSheet.getRange(1, 1, ModeratorValues.length, ModeratorValues[0].length);
destinationRange.setValues(ModeratorValues);
I used the code in a different function as a test. To make it easier
LOCALE = MODERATOR STATUS
FEED = MODERATOR
If there are no duplicate [17]s with different [1]s in the LocationsRefValues, you can reduce the computational complexity from O(n ^ 2) to O(n) by creating a mapping object for LocationsRefValues beforehand, whose keys are the LocationsRefValues[L][17]s and whose values are the LocationsRefValues[L][1]s:
var locationObj = {};
for (var L = 0; L < LocationsRefValues.length; L++) {
locationObj[LocationsRefValues[L][17]] = LocationsRefValues[L][1];
}
for (var F = FeedRefValues.length - 1; F >= 0; F--) { // for each row in the "Feed" sheet...
var feedVal97 = FeedRefValues[F][97];
if (locationObj[feedVal97] != FeedRefValues[F][10]) {
FeedDataSheet.getRange(F + 2, 10 + 1).setValue(locationObj[feedVal97]);
}
}
Thanks #TheMaster, you can speed this up by calling setValue only once, at the end, rather than calling it in a loop, probably something along the lines of:
var locationObj = {};
for (var L = 0; L < LocationsRefValues.length; L++) {
locationObj[LocationsRefValues[L][17]] = LocationsRefValues[L][1];
}
var feedValues = FeedDataSheet.getValues();
for (var F = FeedRefValues.length - 1; F >= 0; F--) { // for each row in the "Feed" sheet...
var feedVal97 = FeedRefValues[F][97];
if (locationObj[feedVal97] != FeedRefValues[F][10]) {
feedValues[F + 2][10 + 1] = locationObj[feedVal97];
}
}
var destinationRange = ss.getRange(1, 1, feedValues.length, feedValues[0].length);
destinationRange.setValues(feedValues);
you can use onEdit(e) trigger to get the reference to the edited cell. In that case you won't need to iterate over the entire Locale" sheet:
function onEdit(e) {
var range = e.range; // edited cell
var rowIndex = range.getRow()
var colIndex = range.getColumn()
if (rowIndex >= LocaleRange.startRow && rowIndex <= LocaleRange.EndRow &&
colIndex >= LocaleRange.startColumn && colIndex <= LocaleRange.EndColumn) {
var index = rowIndex - LocaleRange.startRow
var keyValue = LocationsRefValues[index][17]
var newLocValue = range.getValue()
var newFeedValues = FeedRefValues.map(function (row) {
return (row[97] == keyValue) newLocValue ? : row[10]
})
FeedDataRange.setValues(newFeedValues)
}
}
Here are docs on using onEdit trigger: https://developers.google.com/apps-script/guides/triggers

ActiveXObject extract to Excel formated HTML

I'm trying to send data from json to the Excel File.
Unfortunately to work in IE I use ActiveXObject.
The problem is that my json contain the whole html tags example
Home
or
<p><b>Welcome</b><br/>How are you today</p>
Extraction is working almost perfect. Unfortunately in my Excel are available all html tags. And cells are looking exactly like I wrote.
I want to convert them somehow to look like this
Home
and
Welcome
How are you today
Is it possible to send them already formatted to the Excel or somehow to format the cell into the Excel?
Here is my JS code:
function extractExcel() {
var excApp = new ActiveXObject("Excel.Application");
var excBook = excApp.Workbooks.open("www.localhost/media/excel_template.xlsx");
excApp.visible = true;
excBook.Add
var i = 1;
var j = 0;
$.each($scope.filteredItems, function (index, item) {
$.each(item, function (key, value) {
excApp.Cells(i + 1, j + 1).Value = value;
j++;
});
j = 0;
i++;
});
}

Javascript get all files minus certain file extentions

I have a script that returns all the files contained within a folder. However, there are some file types in there that I do not want my script to do anything with. I just want it to literally skip over it as if it wasn't there and only deal with the other file types.
How can I achieve this?
So far this is how I'm getting all the files contained within a folder:
var samplesFolder = Folder(Path)
//Get the files
var fileList = samplesFolder.getFiles()
//Creat Array to hold names
var renderTypes = new Array();
//Parse Initial name to get similar render elements
var beautyRender = fileList[0].name
beautyRender = beautyRender.substr(0, beautyRender.length-4)
//Get the render elements with a similar name
for (var i = 0; i < fileList.length; i++)
{
if(fileList[i].name.substring(0,beautyRender.length) === beautyRender)
{
renderTypes[i] = fileList[i].name
}
}
This is not used for web purposes I should hasten to add.
edit
Above is the complete code I have to get all the image files in a folder and bring them into photoshop once the user has selected the folder they want to use. At the moment it is bringing in every single image in the folder when there is a single type I want it to ignore.
You can iterate over the list and only collect those with extensions you care about. i see photoshop so I'll assume image files only:
var distilledFileList = [];
for (var i = 0; i < fileList.length; i++){
if (/\.(?:jpe?g|png|gif|psd)$/i.test(fileList[i].name)){
distilledFileList.push(fileList[i]);
}
}
Now distilledFileList contains only *.jpg, *.jpeg, *.png, *.gif, and *.psd files.
if you want an easier (more readable) way to check extensions (maybe you're not as fluent as regular expressions):
// fileList = ....
// setup an array of bad extensions here:
var bad = ['txt', 'log', 'db'],
// holds new list of files that are acceptable
distilledFileList = [];
// iterate over entire list
for (var i = 0; i < fileList.length; i++){
// grab the file extenion (if one exists)
var m = fileList[i].name.match(/\.([^\.]+)$/);
// if there is an extenions, make sure it's now in the
// 'bad' list:
if (m && bad.indexOf(m[1].toLowerCase()) != -1){
// it's safe, so add it to the distilled list
distilledFileList.push(fileList[is]);
}
}
Assuming fileList is just an array of strings you could do something along the lines of:
for (var i = 0, len = fileList.length; i < len; i++) {
var filename = fileList[i].name;
if (filename.match(/\.(txt|html|gif)$/i) !== null) { continue; }
// Your logic here
}
Where txt, html and gif are file extensions you want to skip over, you can add more by separating them with |

jMeter - XPath Extractor does not let BSF Post Processor (with javascript code) to access multiple extracted matches

I have an XML response from server (SOAP) and I'm getting multiple values in it. XPath extracts all the values and stores them internally like Match_1, Match_2, Match_3, etc.
But I can't access them neither via BSF Post Processor nor via JavaScript code pasted in XML. It just refuses to return values then I address to them like this: ${Match_1}.
Example:
Response returns multiple contentGroupID values.
Debug Sampler reveals them:
contentGroupID=67
contentGroupID_1=67
contentGroupID_2=50
contentGroupID_3=38
contentGroupID_4=54
contentGroupID_5=46
We need to use each of these values in the next single request, so we add a code inside XML in place where we need those values to sit:
${__javaScript
(myOutput =''; var names = {};
for (var i = 1; i <= ${contentGroupID_matchNr}; i++)
{names[i] = "${contentGroupID_" + i + "}";}
for (var j = 1; j <= ${contentGroupID_matchNr}; j++)
{myOutput =
myOutput + '<ns8:forContentGroupId><ns2:id>' + names[j] + '</ns2:id></ns8:forContentGroupId>';},
myOutput)
}
Here we place the part of XML with values <ns8:forContentGroupId><ns2:id>' + ids + '</ns2:id></ns8:forContentGroupId> as many times as matches were found by XPath.
But the magic does not happen. The request is sent as follows:
<ns8:forContentGroupId><ns2:id>${contentGroupID_1}</ns2:id></ns8:forContentGroupId>
<ns8:forContentGroupId><ns2:id>${contentGroupID_2}</ns2:id></ns8:forContentGroupId>
<ns8:forContentGroupId><ns2:id>${contentGroupID_3}</ns2:id></ns8:forContentGroupId>
<ns8:forContentGroupId><ns2:id>${contentGroupID_4}</ns2:id></ns8:forContentGroupId>
<ns8:forContentGroupId><ns2:id>${contentGroupID_5}</ns2:id></ns8:forContentGroupId>
The same happens in case I set BSF Post Processor with JavaScript code and add a variable in XML.
The question is: how the hell can one access those magical values?
P.S. Everything works great then I use ForEach Controller. But the trick is I need to make single request with all the values, not multiple requests one after another.
Found the solution here:
BSF Post Processor witch JavaScript code:
var myOutput = '';
var names = {};
var str;
var value = 0;
var match = vars.get("contentGroupID_matchNr");
for (var i = 1; i <= match; i++)
{
var n = i.toString();
str = 'contentGroupID_' + n;
value = vars.get(str);
names[i] = value;
}
for (var j = 1; j <= match; j++) {
myOutput = myOutput +  '<ns8:forContentGroupId><ns2:id>' + names[j] + '</ns2:id></ns8:forContentGroupId>';
}
vars.put("array", myOutput);
Plus ${array} variable inside XML.
End of story! =)

How can I modify the existing email CSV script to skip blank rows, or rows with a single zero in either field?

I've been using the Google-provided script to generate and email CSVs from Google Sheets. I customised it so that it doesn't ask for a range or a file name. This way it just automatically emails me, periodically, with the contents of my range.
The problem is that the CSV contains empty rows, at the end, that I'd like the script to automatically filter out (which I can't achieve, because I just don't have the knowledge). The reason for this, in turn, is that the range contains empty rows - but there's a good reason for that, which is that the rows in the range sometimes expand, sometimes contract, depending on the underlying data. (The range in fact relates to a pivot table).
As a bonus prize, I'd also really like it to skip rows, if there is a single zero in either of the two columns in the rows. (I ought to be able to filter this out in the pivot table; I can, but then the filters don't work properly if new values appear).
This is an example of how my emailed CSVs are looking at the moment:
0,0
,0
0.65,0
0.75,16900
0.78,2000
0.79,500
0.8,110800
0.83,1200
0.85,20000
0.87,4500
0.9,3500
1,5000
1.1,4000
1.2,41500
,
,
,
,
,
,
,
,
,
,
,
,
,
,
This is an example of how I would like to receive that CSV:
0.75,16900
0.78,2000
0.79,500
0.8,110800
0.83,1200
0.85,20000
0.87,4500
0.9,3500
1,5000
1.1,4000
1.2,41500
Any help with this would be HUGELY appreciated. Thanks.
Here's the script I'm using currently:
var settings = {
"recipients": "myemailaddress",
"emailSubject": "CSV file",
"emailMessage": "Your CSV file is attached",
"fileExtension": ".csv",
"carriageReturn": "\r\n"
};
function onOpen() {
var subMenus = [];
subMenus.push({name: "Email Named Range as CSV", functionName: "run"});
SpreadsheetApp.getActiveSpreadsheet().addMenu("CSV", subMenus);
}
function run() {
/var namedRange = Browser.inputBox("Enter named range to convert to CSV (e.g. sampleDataRange):");/
var namedRange = "FORCSVEXPORT";
var fileName = "EPCSELLOFFERS.CSV";
if (namedRange.length !== 0 && fileName.length !== 0) {
settings.dataRangeName = namedRange;
settings.csvFileName = fileName + settings.fileExtension;
var csvFile = convertNamedRangeToCsvFile_(settings.dataRangeName, settings.csvFileName);
emailCSV_(csvFile);
}
else {
Browser.msgBox("Error: Please enter a named range and a CSV file name.");
}
}
function emailCSV_(csvFile) {
MailApp.sendEmail(settings.recipients, settings.emailSubject, settings.emailMessage, {attachments: csvFile});
}
function convertNamedRangeToCsvFile_(rngName, csvFileName) {
var ws = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(rngName);
try {
var data = ws.getValues();
var csvFile = undefined;
if (data.length > 1) {
var csv = "";
for (var row = 0; row < data.length; row += 1) {
for (var col = 0; col < data[row].length; col += 1) {
if (data[row][col].toString().indexOf(",") != -1) {
data[row][col] = "\"" + data[row][col] + "\"";
}
}
// Join each rows columns
// Add carriage return to end of each row
csv += data[row].join(",") + settings.carriageReturn;
}
csvFile = [{fileName: csvFileName, content: csv}];
}
return csvFile;
}
catch(err) {
Logger.log(err);
Browser.msgBox(err);
}
}
Rather than hack the original convertNamedRangeToCsvFile_(), I propose adding an additional step to your run(), that will call a new function to remove the unwanted rows from the csv file. Here it is:
/**
* Remove unwanted lines from given csvFile
*/
function minimizeCsvFile( csvFile ) {
// take apart the csv file contents, into an array of rows
var rows = csvFile[0].content.replace(/\r\n/g, "\n").split("\n");
var newRows = [];
for (var i = 0; i < rows.length; i++ ) {
if (rows[i] == "") continue; // skip blanks
if (rows[i] == ",") continue; // skip null values
// skip rows with either numeric value == 0
var vals = rows[i].split(",");
if (parseFloat(vals[0]) == 0.0 || parseFloat(vals[1]) == 0.0) continue;
// If we got here, we have a keeper - add it to newRows
newRows.push(rows[i]);
}
debugger; // pause to observe in debugger
var csv = newRows.join(settings.carriageReturn);
// Return a single element array with an object, exactly like
// the one from convertNamedRangeToCsvFile_.
return [{fileName: csvFile[0].fileName, content: csv}];
}
To make use of it, change the emailCSV_() line in run() to:
var minimizedCsvFile = minimizeCsvFile(csvFile);
emailCSV_(minimizedCsvFile);
And as for this...
As a bonus prize... Any help with this would be HUGELY appreciated. Thanks.
All we ever want here is for new members of StackOverflow to acknowledge when they receive help! Have a look at this answer for tips on how to accept answers. (You can build rep by asking, answering and accepting answers!)
But I've always wanted a Ferrari...

Categories

Resources