I have long code wherein there is a while loop and inside that while loop there is a for loop and then an if statement. I want that for loop to run again and again until condition of the while loop is fulfilled. Although, that If statement keeps repeating under while loop I am not understanding why that For loop only runs once and never repeats. Please guide what is wrong in the code;
while(affDisc == 'NotClr'){
//Finding affiliate discount row
for(var i = 20; i <= odrEn && slsRprtSh.getRange('I' + i).getValue() > 0 && slsRprtSh.getRange('J' + i).getValue() == 0; i++){
Logger.log(vndr + ' ' + i);
Logger.log(minOdrRw);
if(minOdr == 0){
minOdr = slsRprtSh.getRange('I' + i).getValue();
minOdrRw = i;
}else if(slsRprtSh.getRange('I' + i).getValue() < minOdr){
minOdr = slsRprtSh.getRange('I' + i).getValue();
minOdrRw = i;
}
}
//check if applying discount in minimum order fits here
if(aggFloor - runinFloor > runinFloor + minOdr * .05 - aggFloor && slsRprtSh.getRange('J' + minOdrRw).getValue() == 0){
Logger.log('runfloo ' + runinFloor);
//return false;
slsRprtSh.getRange('J' + minOdrRw).setValue(minOdr * .05);
slsRprtSh.getRange('J' + minOdrRw).setNote('5% discount for affiliate commission');
slsRprtSh.getRange('L' + minOdrRw).setFormulaR1C1('=R[0]C[-3]-R[0]C[-2]');
slsRprtSh.getRange('M' + minOdrRw).setFormulaR1C1('=R[0]C[-1]*0.15');
slsRprtSh.getRange('N' + minOdrRw).setFormulaR1C1('=R[0]C[-1]+R[0]C[-2]');
apldDisc = apldDisc + minOdr * .05;
runinFloor = runinFloor + minOdr * .05;
}else if(aggFloor - runinFloor < runinFloor + minOdr * .05 - aggFloor){
affDisc = 'Clr';
}
}
I am trying to see if i can send an email based on whether or not a value is between two different values. IE: If value is >200 and <239 trigger email.
function sendMaileditbt200239(e){
if (e.range.columnStart != 26 || e.value < 200 > 239) return;
const rData = e.source.getActiveSheet().getRange(e.range.rowStart,1,1,44).getValues();
let firstname = rData[0][3]; //row of first name
let lastname = rData[0][2]; //row of last name
let tbcid = rData[0][1]; //row of TEA ID number
let phnum = rData[0][4]; //row of TEA ID number
let now = new Date().toLocaleString("en-US");
let msg = "Status: BILLING Name: " + firstname + " " + lastname + " Tel:" + phnum + " TBC ID:
" + tbcid + " Approved at " + now + ".";
Logger.log(msg);
MailApp.sendEmail("email#email.com", "Greater Then 200 less than 249", msg);
}
function sendMaileditbt200239(e) {
if (e.range.columnStart == 26 && e.value > 200 && e.value < 239) {
const rData = e.source.getActiveSheet().getRange(e.range.rowStart, 1, 1, 5).getValues();
let firstname = rData[0][3]; //row of first name
let lastname = rData[0][2]; //row of last name
let tbcid = rData[0][1]; //row of TEA ID number
let phnum = rData[0][4]; //row of TEA ID number
let now = new Date().toLocaleString("en-US");
let msg = "Status: BILLING Name: " + firstname + " " + lastname + " Tel:" + phnum + " TBC ID:
" + tbcid + " Approved at " + now + ".";
Logger.log(msg);
MailApp.sendEmail("email#email.com", "Greater Then 200 less than 249", msg);
}
}
I have successfully accessed a Google calendar event and can change the color, but I'm stuck when it comes to removing the hangouts link.
I'm attempting to use code to automatically remove the hangouts links when I am the meeting originator and have not changed the hangout name, but I'm not having any success in actually changing the link.
Any help greatly appreciated.
function removehangout() {
var calendarId = 'primary';
//var calendars = CalendarApp.getOwnedCalendarsByName('Mirage ELDRIDGE');
var now = new Date();
// Determines how many events are happening in the next 24 hours x 1 days.
var now = new Date();
var endr = new Date(now.getTime() + (1 * 24 * 60 * 60 * 1000));
var events2 = CalendarApp.getDefaultCalendar().getEvents(now, endr);
Logger.log('Number of events: ' + events2.length);
var events = Calendar.Events.list(calendarId, {
timeMin: now.toISOString(),
singleEvents: true,
orderBy: 'startTime',
maxResults: 5
});
if (events.items && events.items.length > 0) {
for (var i = 0; i < events.items.length; i++) {
var event1 = events.items[i];
var d = event1.description;
var ttitle = event1.summary;
var whoby = event1.creator.email;
Logger.log(ttitle + ' by: ' + whoby);
if(whoby.indexOf('mirage.eldridge') != -1){
Logger.log(ttitle + '--> was by mirage');
var hangoutname = event1.hangoutLink;
Logger.log(ttitle + '--> hangout link name --> ' + hangoutname);
if (hangoutname != null) {
if (hangoutname.indexOf('mirage-eldridge') != -1){
//delete this link here
Logger.log(ttitle + '--> remove hangout');
//event.setHangoutLink(null);
//var idno = event.iCalUID
//CalendarApp.getEventSeriesById(idno)
event1.HangoutLink = null;
Logger.log(ttitle + '... ' + event1.hangoutLink);
Logger.log(event1.iCalUID);
//event.setcolorId('11');
var event2 = Calendar.Events.get(calendarId, event1.id)
event2.colorId = 9;
event2.hangoutLink = 'fred';
//Calendar.Events.patch(event2,calendarId,event1.id);
Calendar.Events.update(event2,calendarId,event1.id);
}
} else {
Logger.log(ttitle + ' -- do not remove ' + hangoutname);
}
}
if (!d)
d = '';
//var foundlinkyes = d.indexOf('HangoutLink');
//var actuallink = event.hangoutLink;
//var hasrlink = 'True';
//if (!actuallink) {
//Logger.log(ttitle + ' no link found');
//hasrlink = "False";
//}
//Logger.log('desc: ' + ttitle + '-- foundyes: ' + foundlinkyes);
//if (foundlinkyes == -1 && hasrlink == 'True'){
//if (event.hangoutLink && (d.indexOf('Hangout: ')== -1)){
//Logger.log (event.summary + ' - ' + event.hangoutLink + ' - ' + event.description);
//Logger.log(ttitle + ' added this ' + event.hangoutLink);
//event.description = 'HangoutLink: ' + event.hangoutLink + '\n\n' + d;
//Calendar.Events.update(event, calendarId, event.id);
//foundlinkyes = 0;
//hasrlink = 'True';
//}
//}
}
} else {
Logger.log('No events found.');
}
}
So I have been working on a program that ask the user to input a values and when the user exits the code by entering -99 its suppose to return the total Value of all the numbers and the average but I'm stumped My values keep getting overided by the previous one... here is my code
{
var user_Input;<br>
var output_msg;<br>
var count;<br>
var highest;<br>
var value;<br>
var total;<br>
count = 0;
do
{
user_Input = get_integer("Enter a number To add To the List(Enter -99 to leave)",-99);
if (user_Input == (-99)){
continue}
count += 1;
value = 0;
value = value + user_Input;
average = value/count;
}
until(user_Input == (-99)){
count -=1;
user_Input = user_Input + 99;
output_msg=("#Numbers Entered: " + string(count) + "##Total value of numbers: " + string(highest) + "## Average:" + string(average));`
}
show_message(output_msg);
}
How Do I make it so it doesn't override the previous one?
This is because you are setting value equal to 0 every time the while loop is executed. Try setting
value = 0;
before you start the do until loop. Perhaps right after
count = 0;
like this:
count = 0;
value = 0;
do{
user_Input = get_integer("Enter a number To add To the List(Enter -99 to leave)",-99);
if (user_Input == (-99)){continue}
count += 1;
value = value + user_Input;
average = value/count;
}
I'm working on a Chrome Extension that uses WebSQL to store historical data.
Being WebSQL, the DB is stored on the client.
I'd like to add an option to export/import such data so that the user can share/use this data with other users, or with other PCs.
These are my first steps on a client-only database, so I wonder how to do this.
I was thinking to convert the DB to a huge json string that the user can copy/paste but doesn't look very user-friendly.
Is there any better solution?
I got a single table dump solution working on a HTML5 database client I wrote a few days ago.
Check out http://html5db.desalasworks.com/script.js and scroll down to SqlClient.exportTable, this has an example that needs to be expanded to cover the whole database.
The steps are:
Step 1: Create the schema:
SELECT sql FROM sqlite_master
Step 2: Get a list of tables:
SELECT tbl_name from sqlite_master WHERE type = 'table'
Step 3: Loop through each of them and create an INSERT script with the results
transaction.executeSql("SELECT * FROM " + _tbl_name + ";", [],
function(transaction, results) {
if (results.rows) {
for (var i = 0; i < results.rows.length; i++) {
var row = results.rows.item(i);
var _fields = [];
var _values = [];
for (col in row) {
_fields.push(col);
_values.push('"' + row[col] + '"');
}
_exportSql += ";\nINSERT INTO " + _tbl_name + "(" + _fields.join(",") + ") VALUES (" + _values.join(",") + ")";
}
}
}
);
Hope this is useful.
UPDATE JAN 2016 - WHOLE DB EXPORT
I've got a JS websqldump library that you can download from github as well.
This one will export the whole database. Check out the code on:
https://github.com/sdesalas/websqldump
Usage as follows
websqldump.export({
database: 'NorthwindLite',
success: function(sql) {alert(sql);}
});
Not the most elegant way, yet most convenient.
Just paste the script in chrome debugger tools then call c(), and you should get the file.
var looongSQL = "";
var x = function (options) {
if (options.n < options.sqlTables.length) {
onTheMove.openLocalDatabase().transaction(
function (tx) {
var sqlStatement = "SELECT * FROM " + options.sqlTables[options.n];
tx.executeSql(sqlStatement, [],
function (tx, rslt) {
if (rslt.rows) {
for (var m = 0; m < rslt.rows.length; m++) {
var dataRow = rslt.rows.item(m);
var _fields = [];
var _values = [];
for (col in dataRow) {
_fields.push(col);
_values.push('"' + dataRow[col] + '"');
}
looongSQL += "INSERT INTO " + options.sqlTables[options.n] + "(" + _fields.join(",") + ") VALUES (" + _values.join(",") + ");\n";
}
}
options.n++;
x(options);
}
);
});
}else
{
document.location = 'data:Application/octet-stream,' +
encodeURIComponent(looongSQL);
}
};
var c = function () {
onTheMove.openLocalDatabase().transaction(
function (transaction) {
transaction.executeSql("SELECT sql FROM sqlite_master;", [],
function (transaction, results) {
var sqlStatements = [];
if (results.rows) {
for (var i = 0; i < results.rows.length; i++) {
console.log(results.rows.item(i));
var row = results.rows.item(i);
if (row.sql != null && row.sql.indexOf("CREATE TABLE ") != -1 && row.sql.indexOf("__") == -1) {
var tableName = row.sql.replace("CREATE TABLE ", "").split(/ |\(/)[0];
sqlStatements.push('DROP TABLE IF EXISTS ' + tableName);
}if(row.sql != null && row.sql.indexOf("__") == -1){
sqlStatements.push(row.sql);}
}
}
for (var j = 0; j < sqlStatements.length; j++) {
if (sqlStatements[j] != null) {
looongSQL += sqlStatements[j] + ';\r\n';
}
}
transaction.executeSql("SELECT tbl_name from sqlite_master WHERE type = 'table'", [],
function (transaction, res) {
var sqlTables = [];
for (var k = 0; k < res.rows.length; k++) {
if (res.rows.item(k).tbl_name.indexOf("__") == -1) {
sqlTables.push(res.rows.item(k).tbl_name);
}
}
x({
sqlTables: sqlTables,
n: 0
});
});
}
);
});
};
Another version that exports it as JSON
var looongSQL = "[\n";
var stringidiedLocalStorage = JSON.stringify(JSON.stringify(localStorage));
looongSQL += "/* 1 */ " + stringidiedLocalStorage + ",\n";
var x = function (options) {
if (options.n < options.sqlTables.length) {
onTheMove.openLocalDatabase().transaction(
function (tx) {
var sqlStatement = "SELECT * FROM " + options.sqlTables[options.n];
tx.executeSql(sqlStatement, [],
function (tx, rslt) {
if (rslt.rows && rslt.rows.length > 0) {
var _fields = [];
for (var col in rslt.rows.item(0)) {
_fields.push(col);
}
var insertTableSQL = "\"INSERT INTO " + options.sqlTables[options.n] + "(" + _fields.join(",") + ") ";
looongSQL += "/* " + options.count + " */ " + insertTableSQL;
for (var m = 0; m < rslt.rows.length; m++) {
var dataRow = rslt.rows.item(m);
var _values = [];
for (var col in dataRow) {
_values.push('\'' + dataRow[col] + '\'');
}
looongSQL += "SELECT " + _values.join(",");
if (m < rslt.rows.length - 1 && (m % 499 != 0 || m == 0)) {
looongSQL += " UNION ALL ";
}
if (m % 499 == 0 && m != 0) {
options.count++;
looongSQL += "\",\r\n/* " + options.count + " */ " + insertTableSQL;
}
}
looongSQL += "\",\r\n";
options.count++;
}
options.n++;
x(options);
}
);
});
} else {
looongSQL += ']';
document.location = 'data:Application/octet-stream,' +
encodeURIComponent(looongSQL);
}
};
var c = function () {
onTheMove.openLocalDatabase().transaction(
function (transaction) {
transaction.executeSql("SELECT sql FROM sqlite_master;", [],
function (transaction, results) {
var sqlStatements = [];
var count = 2;
if (results.rows) {
for (var i = 0; i < results.rows.length; i++) {
console.log(results.rows.item(i));
var row = results.rows.item(i);
if (row.sql != null && row.sql.indexOf("CREATE ") != -1) {
var objectType = row.sql.replace("CREATE ", "").split(/ |\(/)[0];
if (row.sql.indexOf("CREATE " + objectType + " ") != -1 && row.sql.indexOf("__") == -1) {
var objectName = row.sql.replace("CREATE " + objectType + " ", "").split(/ |\(/)[0];
sqlStatements.push('/* ' + count + ' */ "DROP ' + objectType + ' IF EXISTS ' + objectName + '"');
count++;
}
if (row.sql != null && row.sql.indexOf("__") == -1) {
sqlStatements.push('/* ' + count + ' */ "' + row.sql.replace(/(\r\n|\n|\r)/gm, " ") + '"');
count++;
}
}
}
}
for (var j = 0; j < sqlStatements.length; j++) {
if (sqlStatements[j] != null) {
looongSQL += sqlStatements[j] + ',\r\n';
}
}
transaction.executeSql("SELECT tbl_name from sqlite_master WHERE type = 'table'", [],
function (transaction, res) {
var sqlTables = [];
for (var k = 0; k < res.rows.length; k++) {
if (res.rows.item(k).tbl_name.indexOf("__") == -1) {
sqlTables.push(res.rows.item(k).tbl_name);
}
}
x({
sqlTables: sqlTables,
n: 0,
count: count
});
});
}
);
});
};