wNumb formatting stops noUiSlider price slider working - url

I have a price slider on my site and price range is displayed as £0.0 - £5,000,000.0 with a trailing zero.
I have change the number format code from:
format: wNumb({
decimals: 1
})
to:
format: wNumb({
decimals: 0,
thousand: ','
})
Now this price is being displayed as £ 0 - £ 5,000,000 which is what I want.
The problem now is when doing a search the price in the url is displaying the ascii encoding for a comma %2C and it's not filtering the search results
adv_filter_price_min=0&adv_filter_price_max=5%2C000%2C000
How can I get it so the ascii encoding is not used in the url? eg:
adv_filter_price_min=0&adv_filter_price_max=5000000
Many Thanks
S
Full Code
var rangeslider = jQuery('.rangeslider');
var rangecontainer = rangeslider.parent('.rangeslidercontainer');
var txtlowprice = rangecontainer.children('.adv_filter_price_min');
var txthiprice = rangecontainer.children('.adv_filter_price_max');
var spnlowprice = rangecontainer.find('.text_price_min');
var spnhiprice = rangecontainer.find('.text_price_max');
var defminprice = Number(interfeis_var.search_minprice);
var defmaxprice = Number(interfeis_var.search_maxprice);
var hdnminprice = txtlowprice;
var hdnmaxprice = txthiprice;
var minprice = Number(hdnminprice.val());
var maxprice = Number(hdnmaxprice.val());
if(rangeslider.length){
rangeslider.noUiSlider({
start: [ minprice, maxprice ],
step: 10000,
range: {
'min': defminprice,
'max': defmaxprice
},
connect: true,
// Set some default formatting options.
// These options will be applied to any Link
// that doesn't overwrite these values.
format: wNumb({
decimals: 0
})
});
rangeslider.Link('lower').to(txtlowprice);
rangeslider.Link('upper').to(txthiprice);
rangeslider.Link('lower').to(spnlowprice, setFormat);
rangeslider.Link('upper').to(spnhiprice, setFormat);
}

You are using wNumb to format the input passed to slider, so when you try to fetch data back from slider, it returns you formatted inputs.
In Your case, when you formatted it using thousand: ',' , it make a number 50000 to 50,000 (Notice the comma). So when you try to fetch it back from slider it returns you 50,000 instead of 50000.
So I suggest you to do following to unformat the numbers back.
Create wnumb object to pass it to slider
var thousand_format = wNumb({
decimals: 0,
thousand: ','
});
When you get input back from slider after using slider unformat it using thousand_format object using wnumb's from method.
var unformat_input = thousand_format.from( '50,000' ); // it will return 50000
Same unformat_input you use to set in url. wNumb causes slider to not work properly as wNumb requires numbers(50000) and after your first usage you must be passing it string(50,000).I hope by unformating it should fix the issue.
Know more about wNumb here

Related

Google Sheets Error: The parameters (String) don't match the method signature for SpreadsheetApp.Sheet.getActiveCell

Trying to move data from one sheet to another if two sets of data in each sheet corresponds (the date in this case). I keep getting the following error:
Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Sheet.getActiveRange
I've seen some things to suggest I might not be pulling through from the sheet I've named, or that it's pulling through the value wrong? Not sure, any advice would be great.
Code:
function pullData(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var inputSheet = ss.getSheetByName("Input");
var currentSheet = ss.getActiveSheet();
var dateCell = inputSheet.getActiveCell("C2").getValue();
var inputRange = inputSheet.getActiveRange("C6:Z999");
var currentRange = currentSheet.getActiveRange("C6:Z999");
if (dateCell == currentSheet.getActiveCell("B2").getValue()){
var inputRows = inputRange.getNumRows();
var inputCols = inputRange.getNumColumns();
for (var i = 1; i <= inputRows; i++) {
for (var j = 1; j <= inputCols; j++) {
var inputValue = inputRange.getCell(i,j).getValue();
var currentValue = currentRange.getCell(i,j).setValue(inputValue);
}
}
}
}
When the value of cell "C2" of the sheet Input is the same with the value of cell "B2 of the active sheet, you want to copy the values of cells "C6:Z999" of the sheet Input to the cells "C6:Z999" of the active sheet.
You want to know the reason of the following error message.
Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Sheet.getActiveRange
Modification points:
getActiveRange() has no arguments. But you use the arguments. I think that the reason of your error message is this.
Also, getActiveCell() has no arguments. So in your script, I think that an error occurs at var dateCell = inputSheet.getActiveCell("C2").getValue();. From this situation, I thought that your tested script might be different from the script in your question.
When I saw the flow of your script, I thought that your goal might be as follows.
When the value of cell "C2" of the sheet Input is the same with the value of cell "B2 of the active sheet, you want to copy the values of cells "C6:Z999" of the sheet Input to the cells "C6:Z999" of the active sheet.
If my understanding is correct, getActiveCell("C2"), getActiveRange("C6:Z999") and getActiveCell("B2") might be getRange("C2"), getRange("C6:Z999") and getRange("B2"), respectively.
Pattern 1:
In this pattern, your script is modified for removing the error message.
Modified script:
Please modify your script as follows.
From:
var dateCell = inputSheet.getActiveCell("C2").getValue();
var inputRange = inputSheet.getActiveRange("C6:Z999");
var currentRange = currentSheet.getActiveRange("C6:Z999");
if (dateCell == currentSheet.getActiveCell("B2").getValue()){
To:
var dateCell = inputSheet.getRange("C2").getValue();
var inputRange = inputSheet.getRange("C6:Z9");
var currentRange = currentSheet.getRange("C6:Z9");
if (dateCell == currentSheet.getRange("B2").getValue()){
Pattern 2:
In this pattern, your script is modified by reducing the process cost. In your current script, getValue() and setValue() are used in the for loop. In this case, when inputRows and inputCols are large, the process cost will be high. So in this pattern, I would like to propose to reduce the cost.
Modified script:
Please modify your script as follows. In this modification, the values of cells "C6:Z999" of the sheet Input are copied to the cells "C6:Z999" of the active sheet using copyTo. By this, your goal can be achieved without using the for loop.
function pullData(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var inputSheet = ss.getSheetByName("Input");
var currentSheet = ss.getActiveSheet();
var dateCell = inputSheet.getRange("C2").getValue();
var inputRange = inputSheet.getRange("C6:Z9");
var currentRange = currentSheet.getRange("C6:Z9");
if (dateCell == currentSheet.getRange("B2").getValue()){
inputRange.copyTo(currentRange, {contentsOnly:true}); // Modified
}
}
References:
getActiveRange()
getActiveCell()
copyTo(destination, options)

Search entire column for string, return all row values

Trying to to write a script to search through column A starting at the top, and return all values of the row on which it matches a string. I want to have it output the values in plain html, while the string would be defined from a parameter on the url line.
function doGet(e) {
var param = e.parameter.param;
var sheet = SpreadsheetApp.openById("SHEETID").getSheetByName("Sheet1");
var column = sheet.getRange("A");
var values = column.getValues();
var row = 0;
while ( values[row] && values[row][0] !== param ) {
row++;
}
if (values[row][0] === param)
var output = row.getValues()
return ContentService.createTextOutput(output);
}
Other errors as well while changing code...
Range not found (line 5, file "Code", project "Column Search")
Unfortunately You cannot retrieve a range as getRange("A")
You cannot modify your code to
var column = sheet.getRange("A:A");
However keep in mind that this will make your code very slow. Consider
retrieving only the range that actually has contents:
var lastRow=sheet.getLastRow();
var column = sheet.getRange(1,1,lastRow,1);
References:
getRange(row, column, numRows, numColumns)
getLastRow()

retrieving id and incrementing it swift3

I have a id whose format is 1223-3939-ABC.1 and I would like to retrieve the last value i.e. 1 and increment it so now it looks like 1223-3939-ABC.2. But its possible that "1" is not there so in that case, I would like to append ".1"
I am trying to achieve this in Swift and here is my code:
var deviceId: String = "1234-ASCD-SCSDS.1"
if (deviceId != "") {
var id: [String] = deviceId.components(separatedBy: ".")
if let incrementedId: String = id.capacity > 1 ? deviceId.components(separatedBy: ".")[1] : "" {
if (incrementedId == "") {
//should append to id
var firstEle = deviceId.components(separatedBy: ".")[0]
firstEle.append(".")
firstEle.append("1")
deviceId = firstEle
} else {
// retrieve that id, convert to int, increment id, convert back to string and replace the old id with new id
let newId: Int = Int(deviceId.components(separatedBy: ".")[1])! + 1
deviceId = deviceId.replacingOccurrences(of: ".\\d", with: ".\(newId)", options: .regularExpression)
}
}
}
Not sure what I'm doing wrong?
Your regular expression for replacing is .\\d where . is actually any symbol. Replace it with \\.\\d and it will operate as expected.
You are referencing capacity but you need to reference count to understand an amount of components.
Based on documentation:
Capacity – the total number of elements that the array can contain without
allocating new storage.
There are several problems, such as
Wrong usage of capacity (as already said by Nikita),
Your code assumes that there is only a single dot, so that id
has exactly two elements.
Your code will crash if the dot is not followed by an integer.
The main problem is that
deviceId = deviceId.replacingOccurrences(of: ".\\d", with: ".\(newId)", options: .regularExpression)
replaces all occurrences of an arbitrary character followed by
any digit with ".\(newId)". It should probably be
deviceId = id[0] + ".\(newId)"
instead.
But the entire problem can be solved much easier:
Find the last occurrence of a dot.
Check if the part of the string following the dot can be converted to an integer.
If yes, replace that part by the increased integer, otherwise append .1
Both checks can be achieved with conditional binding, so that the
if-block is only executed if the device id already has a trailing
number:
var deviceId = "1234-ASCD-SCSDS.1"
if let pos = deviceId.range(of: ".", options: .backwards),
let id = Int(deviceId.substring(from: pos.upperBound)) {
deviceId = deviceId.substring(to: pos.upperBound) + String(id + 1)
} else {
deviceId = deviceId + ".1"
}
print(deviceId) // 1234-ASCD-SCSDS.2

How to create an "Open in Google Sheets" button

I already have an "export to CSV" button on my site. But I'd like to have an "Open in Google Sheets" button, which opens the CSV directly into Google Sheets.
That'll save the user a few steps, so they will no longer have to (1) download the CSV, and (2) import it into Google Sheets.
Any way to do this?
If you wanted to avoid the full API . . .
Use Google Script to import the CSV into a Google Sheet and set the script trigger to run on a sensible schedule (every, minute, hour, day etc.).
//adapted from http://stackoverflow.com/a/26858202/3390935
function importData() {
var ss = SpreadsheetApp.getActive();
var url = 'HTTP://YOURURL.COM/FILE.CSV';
var file = UrlFetchApp.fetch(url); // get feed
var csv = file.getBlob().getDataAsString();
var csvData = CSVToArray(csv); // see below for CSVToArray function
var sheet = ss.getActiveSheet();
// loop through csv data array and insert (append) as rows into 'NEWDATA' sheet
for ( var i=0, lenCsv=csvData.length; i<lenCsv; i++ ) {
sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
}
};
// http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm
// This will parse a delimited string into an array of
// arrays. The default delimiter is the comma, but this
// can be overriden in the second argument.
function CSVToArray( strData, strDelimiter ) {
// Check to see if the delimiter is defined. If not,
// then default to COMMA.
strDelimiter = (strDelimiter || ",");
// Create a regular expression to parse the CSV values.
var objPattern = new RegExp(
(
// Delimiters.
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
// Quoted fields.
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
// Standard fields.
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
// Create an array to hold our data. Give the array
// a default empty first row.
var arrData = [[]];
// Create an array to hold our individual pattern
// matching groups.
var arrMatches = null;
// Keep looping over the regular expression matches
// until we can no longer find a match.
while (arrMatches = objPattern.exec( strData )){
// Get the delimiter that was found.
var strMatchedDelimiter = arrMatches[ 1 ];
// Check to see if the given delimiter has a length
// (is not the start of string) and if it matches
// field delimiter. If id does not, then we know
// that this delimiter is a row delimiter.
if (
strMatchedDelimiter.length &&
(strMatchedDelimiter != strDelimiter)
){
// Since we have reached a new row of data,
// add an empty row to our data array.
arrData.push( [] );
}
// Now that we have our delimiter out of the way,
// let's check to see which kind of value we
// captured (quoted or unquoted).
if (arrMatches[ 2 ]){
// We found a quoted value. When we capture
// this value, unescape any double quotes.
var strMatchedValue = arrMatches[ 2 ].replace(
new RegExp( "\"\"", "g" ),
"\""
);
} else {
// We found a non-quoted value.
var strMatchedValue = arrMatches[ 3 ];
}
// Now that we have our value string, let's add
// it to the data array.
arrData[ arrData.length - 1 ].push( strMatchedValue );
}
// Return the parsed data.
return( arrData );
};
Make that sheet public and append /copy to the URL.
https://docs.google.com/spreadsheets/d/YOURGDOCID/copy
You can use the Google Sheets API.
This link provides guides for several programming languages, references, samples, etc. https://developers.google.com/sheets/
Good luck.

How to put variable inside text field AND how to convert all elements to string NOT just 1 at a time?

This is a follow up question to How to have 10 characters total and make sure at least one character from 4 different sets is used randomly
this is my code so far
let sets = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz", "1234567890", "\"-/:;()$&#.,?!'[]{}#%^\\|~<>€£¥•.,"].map { Array($0.characters) }
var randoms = sets.map { $0.random }
while randoms.count < 10 {
randoms.append(sets.random.random)
}
var convertedElems = String()
let something = randoms.shuffled()
for key in something {
convertedElems = String(key)
}
uniqueRoomID.text = randoms.shuffled()
Im getting an error saying cannot convert [Element] to type "String"
So i tried a for loop but that only converts 1 at a time when its supposed to do all 10
my other question is i tried storing a character in a variable and then setting a text field.text equal to that variable and nothing happened
What am i doing wrong here
Your randoms.shuffled() is an array of Characters. You need to convert it back into a String.
Change this:
uniqueRoomID.text = randoms.shuffled()
to this:
uniqueRoomID.text = String(randoms.shuffled())

Resources