I'm extracting numbers from web page, and i want compare them, if
number is more than [previous one, then show it in iimDispaly(), otherwise if is it lower then skip, i trued here to do but cannot find a solution.
for(var i = 1; i <= 13; i++) {
for(var j = 3; j<=33; j+=3 ) {
iimPlayCode('TAG POS='+j+' TYPE=DIV ATTR=CLASS:"group_row_labeled" EXTRACT=TXT')
var res = iimGetLastExtract();
var result = res.replace(/[а-z]/g, '');
if(j==3) {
var firstRes = result;
}
if(result => firstRes) {
iimDisplay("Highest Number: " + result)
}
}
}
Copy & paste the following code and try to play it:
for(var i = 1; i <= 13; i++) {
for(var j = 3; j<=33; j+=3 ) {
iimPlayCode('TAG POS='+j+' TYPE=DIV ATTR=CLASS:"group_row_labeled" EXTRACT=TXT')
var res = iimGetLastExtract();
var result = parseFloat(res.replace(/[a-z]/g, ''));
if(j==3) {
var firstRes = result;
}
if(result >= firstRes) {
iimDisplay("Highest Number: " + result)
}
}
}
Related
I have two separate scripts in google sheets that are triggered by "TRUE" value checkboxes. The first script is set to uncheck all checkboxes from the sheet and the second is resetting a dropdown list. I'll post the code below for the two scripts. Ultimately I would like both conditions to run on one checkbox. If the code can be written in a more simplified manor that would not be a bad thing.
Best regards, Jon
Script 1:
function onEdit(e) {
if (e.range.getSheet().getName() == 'Audits') {
if (e.range.getA1Notation() == 'J27' && e.value == "TRUE") {
e.range.setValue("FALSE");
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var rg = sh.getDataRange();
var vA = rg.getDataValidations();
var cbA = [];
for (var i = 0; i < vA.length; i++) {
for (var j = 0; j < vA[i].length; j++) {
var rule = vA[i][j];
if (rule != null) {
var criteria = rule.getCriteriaType();
if (criteria == SpreadsheetApp.DataValidationCriteria.CHECKBOX) {
sh.getRange(i + 1, j + 1).setValue(null)
}
}
}
}
}
}
}
Script 2
function onEdit(e) {
if (e.range.getSheet().getName() == 'Audits') {
if (e.range.getA1Notation() == 'E27' && e.value == "TRUE") {
e.range.setValue("FALSE");
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var rg = sh.getDataRange();
var vA = rg.getDataValidations();
var cbA = [];
for (var i = 0; i < vA.length; i++) {
for (var j = 0; j < vA[i].length; j++) {
var rule = vA[i][j];
if (rule != null) {
var criteria = rule.getCriteriaType();
if (criteria == SpreadsheetApp.DataValidationCriteria.VALUE_IN_LIST) {
sh.getRange(i + 1, j + 1).setValue(null)
}
}
}
}
}
}
}
To combine and simplify your script, I extracted the variable declarations, which are common to both scripts, outside of the if statements, and then directly added the conditional statement for the criteria of the second script to the first criteria using an or statement (||) as shown below:
function onEdit(e) {
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var rg = sh.getDataRange();
var vA = rg.getDataValidations();
if (e.range.getSheet().getName() == 'Audits') {
if (e.range.getA1Notation() == 'E27' && e.value == "TRUE") {
e.range.setValue("FALSE");
for (var i = 0; i < vA.length; i++) {
for (var j = 0; j < vA[i].length; j++) {
var rule = vA[i][j];
if (rule != null) {
var criteria = rule.getCriteriaType();
if (criteria == SpreadsheetApp.DataValidationCriteria.CHECKBOX || criteria == SpreadsheetApp.DataValidationCriteria.VALUE_IN_LIST) {
sh.getRange(i + 1, j + 1).setValue(null)
}
}
}
}
}
}
}
To test the script's functionality, I created the following setup:
When the checkbox in cell E27 is toggled, all of the sample dropdown lists and checkboxes are reset.
I hope this helps.
In the process of writing a regex to obtain the browser and version. This is what I have so far. Can anyone advise a better process? The regex will check for IE/Firefox/Safari/Chrome from this I can check the version number.
import flashx.textLayout.formats.BackgroundColor;
var inputField:TextField = new TextField();
var displayField:TextField = new TextField();
// RegExp
var reg:RegExp = /(MSIE|(?!Gecko.+)Firefox|(?!AppleWebKit.+Chrome.+)Safari|(?!AppleWebKit.+)Chrome|AppleWebKit(?!.+Chrome|.+Safari)|Gecko(?!.+Firefox) (SeaMonkey))(?: |\/)([\d]+)/i;
inputField.border = true;
inputField.width = 500;
inputField.height = 50;
inputField.x = 75;
inputField.y = 50;
inputField.type = "input";
inputField.multiline = true;
addChild(inputField);
displayField.border = true;
displayField.width = 500;
displayField.height = 200;
displayField.backgroundColor = 0x666666;
displayField.x = 75;
displayField.y = inputField.y + 50 + inputField.height;
displayField.multiline = true;
addChild(displayField);
inputField.addEventListener(Event.CHANGE, changeListener, false, 0, true);
function changeListener(event:Event):void
{
var strToTest:String = inputField.text;
var textToDisplay:String = "";
if(reg.test(strToTest))
{
var browser:String = strToTest.match(reg)[1];
var version:int = strToTest.match(reg)[2];
var chromeVersion:int = 29;
var firefoxVersion:int = 23;
var ieVersion:int = 7;
var safariVersion:int = 6;
textToDisplay += "BROWSER IS " + browser;
textToDisplay += "\nVersion " + version;
if(browser == "Chrome")
{
if(version <= chromeVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + chromeVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + chromeVersion;
}
}
else if(browser == "MSIE")
{
if(version <= ieVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + ieVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + ieVersion;
}
}
else if (browser == "Firefox")
{
if(version <= firefoxVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + firefoxVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + firefoxVersion;
}
}
else if (browser == "Safari")
{
if(version <= safariVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + safariVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + safariVersion;
}
}
}
else
{
textToDisplay = "Browser is NOT IE/FIREFOX/SAFARI/CHROME"
}
displayField.text = textToDisplay;
}
(MSIE(?!.+Opera)|Firefox(?!.+SeaMonkey)|Version(?=.+Safari)(?!.+Chrome)|Chrome)(?: |/)([\d]+)
I Simplified the Regex to follow these checks https://developer.mozilla.org/en-US/docs/Browser_detection_using_the_user_agent.
I don't want to use a third party lib as I would like to understand as I may need to make changes. The following code appears to be returning correctly for what I want.
(MSIE(?!.+Opera)|Firefox(?!.+SeaMonkey)|Version(?=.+Safari)(?!.+Chrome)|Chrome)(?: |/)([\d]+)
Given two strings (s1, s2), Levenshtein Distance is the minimum number of operations needed to change s1 to s2 or vice versa.
I want to show the result of changing s1 to s2. For example, changing Sunday to Saturday needs 3 operations. I need to show S++u+day. "+" is for each operations needed.
Here is a javascript snippet that returns what you want. If you are familiar with the dynamic programming algorithm you should be able follow this code. All the string operations/manipulation of the return string r and handling of min/curMin are what's changed from the original version.
function edits(t, s) {
var r = "";
if (s === t) {
return s;
}
var n = s.length, m = t.length;
if (n === 0 || m === 0) {
return "+".repeat(n + m);
}
var x, y, a, b, c, min = 0;
var p = new Array(n);
for (y = 0; y < n;)
p[y] = ++y;
for (x = 0; x < m; x++) {
var e = t.charCodeAt(x);
c = x;
b = x + 1;
var currMin = min;
min = n + 1;
for (y = 0; y < n; y++) {
a = p[y];
if (a < c || b < c) {
b = (a > b ? b + 1 : a + 1);
}
else {
if (e !== s.charCodeAt(y)) {
b = c + 1;
}
else {
b = c;
}
}
if (b < min) {
min = b;
}
p[y] = b;
c = a;
}
if (min > currMin) {
r += "+";
}
else {
r += t[x];
}
}
return r;
}
EDIT: The implementation above is an version optimized for speed and space so might be harder to read. The implemetation below is a modified version of the JS version from Wikipedia and should be easier to follow.
function getEditDistance(a, b) {
if(a.length === 0) return "+".repeat(b.length);
if(b.length === 0) return "+".repeat(a.length);
var matrix = [];
// increment along the first column of each row
var i;
for(i = 0; i <= b.length; i++){
matrix[i] = [i];
}
// increment each column in the first row
var j;
for(j = 0; j <= a.length; j++){
matrix[0][j] = j;
}
var r = "", min = 0;;
// Fill in the rest of the matrix
for(i = 1; i <= b.length; i++){
var currMin = min;
min = a.length + 1;
for(j = 1; j <= a.length; j++){
if(b.charAt(i-1) == a.charAt(j-1)){
matrix[i][j] = matrix[i-1][j-1];
} else {
matrix[i][j] = Math.min(matrix[i-1][j-1] + 1, // substitution
Math.min(matrix[i][j-1] + 1, // insertion
matrix[i-1][j] + 1)); // deletion
}
if (matrix[i][j] < min) {
min = matrix[i][j];
}
}
if (min > currMin) {
r += "+";
}
else {
r += b[i-1];
}
}
return r;
}
EDIT2: Added explanation of the algorithm and example output
Below is the levenshtein matrix from the input strings "kitten" and "sitting". What I changed in the original algorithm is that I added a check if the current rows minimum value is larger then the previous rows minimum, and if it is, there is an edit in the current row and thus adding a "+". If not and the " edit cost" for the current row is the same as the previous. Then there is no edit necessary and we just add the current character to the result string. You can follow the algorithm row by row in the image (starting at row 1, and column 1)
Examples:
> getEditDistance("kitten", "sitting");
'+itt+n+'
> getEditDistance("Sunday", "Saturday");
'S++u+day'
I'm using ASGALLANT'S Hide/Show method shown here:
http://jsfiddle.net/asgallant/6gz2Q/
Except I have fourth data series called average. When a series is hidden or shown, I recalculate the average... by adjusting the fiddle above:
function showHideSeries () {
var sel = chart.getChart().getSelection();
var view = chart.getView() || {};
// if selection length is 0, we deselected an element
if (sel.length > 0) {
// if row is undefined, we clicked on the legend
if (sel[0].row == null) {
var col = sel[0].column;
if (typeof(columns[col]) == 'number') {
var src = columns[col];
// hide the data series
columns[col] = {
label: datatable.getColumnLabel(src),
type: datatable.getColumnType(src),
sourceColumn: src,
calc: function () {
return null;
}
};
//record as hidden
hiddenSeries[col] = true;
// grey out the legend entry
series[columnsMap[src]].color = '#CCCCCC';
chart.setOption('series', series);
//If Exists a columnLabel called "Average", check if last column (average column is currently shown), recalculate average
if ((datatable.getColumnLabel(datatable.getNumberOfColumns() - 1) == 'Average') && (hiddenSeries[datatable.getNumberOfColumns() - 1] == false)){
for(var r = 0; r < datatable.getNumberOfRows(); ++r) {
sum = 0;
k = 0
for(var c = 1; c < datatable.getNumberOfColumns()-1; ++c) {
if(hiddenSeries[c] == false){
if(datatable.getValue(r, c) > 0){
sum = sum + datatable.getValue(r, c);
k = k + 1;
}
}
}
if (k == 0) k = 1;
datatable.setValue(r, datatable.getNumberOfColumns() - 1, sum/k);
}
}
var tmpColumn = new Array();
// Add each data value to the array with push()
for(var i = 0; i < datatable.getNumberOfRows(); ++i) {
tmpColumn.push(datatable.getValue(i, col));
}
}
else {
var src = columns[col].sourceColumn;
// show the data series
columns[col] = src;
series[columnsMap[src]].color = null;
//record as shown
hiddenSeries[col] = false;
//If Exists a columnLabel called "Average", check if last column (average column is currently shown), recalculate average
if ((datatable.getColumnLabel(datatable.getNumberOfColumns() - 1) == 'Average') && (hiddenSeries[datatable.getNumberOfColumns() - 1] == false)){
for(var r = 0; r < datatable.getNumberOfRows(); ++r) {
sum = 0;
k = 0;
for(var c = 1; c < datatable.getNumberOfColumns() - 1; ++c) {
if(hiddenSeries[c] == false){
if(datatable.getValue(r, c) > 0){
sum = sum + datatable.getValue(r, c);
k = k + 1;
}
}
}
if (k == 0) k = 1;
datatable.setValue(r, datatable.getNumberOfColumns() - 1, sum/k);
}
}
//chart.setOption('series.' + i + '.type', 'bars');
if(datatable.getColumnLabel(src) == 'Average' || datatable.getColumnLabel(src) == 'Peak Demand [kVA]'){
chart.getOptions().series[columnsMap[src]].type = 'line';
if(c.second_axis == true){
chart.getOptions().series[columnsMap[src]].targetAxisIndex = 1;
}
}
}
chart.setDataTable(datatable);
view.columns = columns;
chart.setView(view);
google.visualization.events.addListener(chart, 'ready', function() {
document.getElementById('chartImg').href = chart.getChart().getImageURI();
});
chart.draw();
}
}
}
It works great, except that the Standard tooltip is not being updated on the re-draw of the chart. I would like to avoid using custom tooltips as a solution. I would think that the ToolTip values should automatically update on each draw of the chart... if not, there must be a way to force it?
The issue here is that I was using formatted numbers as shown below:
var formatter = new google.visualization.NumberFormat(
{negativeColor: 'red', negativeParens: true, pattern: '###,###'});
formatter.format(datatable, i);
Therefore, not only did I have to use datatable.setValue(row, column, value) but I also have to use datatable.setFormattedValue(row, column, *String* value).
Having the following map:
function (doc, meta) {
emit(dateToArray(doc.timestamp), doc.user_id);
}
What changes to I need on this reduce to get a count of user_ids that occur more than once?
function(key, values, rereduce) {
var counts = {}, id;
for (var i = 0; i < values.length; i++) {
id = values[i].replace('-', '_');
counts[id] = counts[id] || 0;
counts[id] = counts[id] + 1;
}
var total = 0;
for (id in counts) {
if (counts[id] > 1) total++;
}
return total;
}
I tried it on plain JS passing some array of values, and it works as I expected. Debugging is difficult on Couchbase though (or I don't know how), and I'm not seeing any errors on the error.1 and mapreduce_errors.1 files.
Update: I've been doing some crude debugging, going line by line, and this is what I have so far:
function(key, values, rereduce) {
var counts = {}, total = 0;
for (var i = 0; i < values.length; i++) {
var id = values[i];
if (id.indexOf('id_') != 0) id = 'id_' + values[i].replace(/-/g, '_');
return id;
counts[id] = counts[id] || 0;
counts[id] = counts[id] + 1;
}
for (id in counts) {
if (counts[id] > 1) total++;
}
return total;
}
Notice the return id; line. It returns a modified uuid, prefixed by "id_", just like you'd expect by looking at the code.
But then, if I change that line to return counts; or return total;, the result from Couchbase is "The results of this view is empty." What gives?
It was one painful debugging session, but here's the final solution:
function(key, values, rereduce) {
var counts = {}, total = 0, id;
if (rereduce) {
for (var set in values) {
for (var i in values[set]) {
counts[i] = (counts[i] || 0) + values[set][i];
}
}
for (f in counts) {
if (counts[f] > 1) total++;
}
return total;
} else {
for (var i in values) {
id = 'id_' + values[i].replace(/-/g, '_');
counts[id] = (counts[id] || 0) + 1;
}
return counts;
}
}
An extra lesson learned: Couchbase won't return big objects off of a reduce operation. Gotta learn more about that.