Google Apps Scripting Running a Script Based on a Variable - google-apps-script

Okay, I've been a coding for a little while and have had the chance to do a number of things.
One issue I'd like to find a more elegant way is running scripts based on the value of a variable.
At the moment I'm doing it like this.
if (ScriptName == 'overnightRun' ) {
overnightRun();
}
if (ScriptName == 'resetAnalysisSheets' ) {
resetAnalysisSheets();
}
if (ScriptName == 'resetActiveSheetFormulae' ) {
resetActiveSheetFormulae();
}
if (ScriptName == 'saveRemainingContractSheets' ) {
saveRemainingContractSheets();
}
if (ScriptName == 'saveAllContractSheets' ) {
saveAllContractSheets();
}
if (ScriptName == 'saveCurrentContractSheet' ) {
saveCurrentContractSheet();
}
if (ScriptName == 'clearDataSheets' ) {
clearDataSheets();
}
Surely there must be a better way. Something more like this.
run.script(ScriptName);
Anyone any ideas? Thanks for reading!

You could try this:
function testFunction() {
var ScriptName = "Alibaba";
var p = "Open sesame!"
this[ScriptName](p);
}
function Alibaba(p) {
Logger.log(p);
}
Leave the parameter p blank if nothing is to be passed.

Related

Issue with setValues

I have this code :
function calculateTotals(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var total, clientID,cellValues,cellFontLines, result;
for (i=0;i<98;i++) {
//sheet.getRange("K"+i).setValue("calculating");
clientID=sheet.getRange("J3:J100").getValues();
total=0;
cellValues=sheet.getRange("B3:H234").getValues();
cellFontLines=sheet.getRange("B3:H234").getFontLines();
result=sheet.getRange("K3:K100").getValues(); // this is dumb
if (clientID[i][0] != "") {
for (j=0;j<=6;j++) {
for (k=0;k<=231;k++) {
if (cellValues[k][j] == clientID[i][0] && cellFontLines[k][j] != 'line-through' ) {
total++;
}
}
}
result[i][0]=total/2;
Logger.log(i,clientID[i][0],result[i][0]);
//sheet.getRange("K"+(i+3)).setValue(result[i][0]);
} else {
break;
}
}
sheet.getRange("K3:K100").setValues(result); // this doesn't seem to do anything
Logger.log(result);
}
I can't figure out how to use setValues. I logged it all, and it looks right, in the debugger the array is all set but I still have to use the commented out setValue() line to update that column.
As an aside, is there a better way to initialize result to the correct dimensions than just reading in the previous results?
This works :
function calculateTotals(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var total, clientID,cellValues,cellFontLines, result=new Array(98);
clientID=sheet.getRange("J3:J100").getValues();
cellValues=sheet.getRange("B3:H234").getValues();
cellFontLines=sheet.getRange("B3:H234").getFontLines();
result=sheet.getRange("K3:K100").getValues();
for (i=0;i<98;i++) {
//sheet.getRange("K"+i).setValue("calculating");
total=0
if (clientID[i][0] != "") {
for (j=0;j<=6;j++) {
for (k=0;k<=231;k++) {
if (cellValues[k][j] == clientID[i][0] && cellFontLines[k][j] != 'line-through' ) {
total++;
}
}
}
result[i][0]=total/2;
} else {
break;
}
}
sheet.getRange("K3:K100").setValues(result);
}

HTML5 number input field goes up very fast in Chrome

The issue I am having is on a booking form, where there are several number input fields. They have the up and down arrows which is fine, but when using the up arrow in Chrome, rather than going up by 1 it goes really easily up by several numbers at a time (without holding down the mouse).
Has anyone else experienced this? Is there a fix for it other than hiding it in Chrome?
Thanks
To add, here is the full code with event handler:
$(".product_holder input").bind('keyup change click', function (e) {
if ($('#startDate').val() != "" && $('#endDate').val() != "") {
if (!$(this).data("previousValue") || $(this).data("previousValue") != $(this).val() ) {
if ($(this).is("[max]") && ( $(this).val() >= $(this).attr('max') ) )
{
if (! $(this).parent().find(".validwarnning").length > 0)
{
$(this).parent().append("<div class='validwarnning'>The maximum number has been reached</div>");
}
$(this).val($(this).attr('max'));
}
else
{
$(this).data("previousValue", $(this).val());
//$(this).parent
$thisProduct = $(this).parent();
WriteItemRow($thisProduct);
updateTableTotal();
}
}
}
else
{
$('#startDate').addClass("error_input");
$('#endDate').addClass("error_input");
$('#dateError').html("Please select your dates first");
$(this).val("0");
}
});
You should include a step attribute on the input tag step='1'

how does the "for each in" loop work?

for some reason this code does not work as intended, I've cut out most of my program but, all I think I need to tell you is that I have a tree array, and I am trying to make collisions with said trees so I thought that this would work:
function collisions(loopEvent:Event):void
{
for each (var a:tree in TreeArray)
{
if (brettMc.right1.hitTestObject(a.stump))
{
rightcoll = false;
}
else
{
rightcoll = true;
}
if (brettMc.left1.hitTestObject(a.stump))
{
leftcoll = false;
}
else
{
leftcoll = true;
}
if (brettMc.up1.hitTestObject(a.stump))
{
upcoll = false;
}
else
{
upcoll = true;
}
if (brettMc.down1.hitTestObject(a.stump))
{
downcoll = false;
}
else
{
downcoll = true;
}
}
}
I am pretty sure that the problem is just that the for each loop is messed up.
I don't see any issue with loop itself
EXCEPT
Is TreeArray actual name of variable? Or it's a Type?
If that's actual variable, can you trace the length of it before loop?

as3 reset the 'numPressed' function

i'm quite new to as3 so this may be pretty obvious to most of you out there :P
I'm using the numPressed function (which counts the mouse clicks) and need to reset the mouse clicks after the 6th click...
This is what the code looks like:
var numPressed:Number = 0;
any_mc.addEventListener(MouseEvent.CLICK, countUp);
function countUp(evt:MouseEvent):void {
numPressed++;
if (numPressed == 1) {
any_mc.gotoAndPlay(1);
}
else if (numPressed == 2) {
any_mc.gotoAndPlay(2);
}
else if (numPressed == 3) {
any_mc.gotoAndPlay(3);
}
else if (numPressed == 4) {
any_mc.gotoAndPlay(4);
}
else if (numPressed == 5) {
any_mc.gotoAndPlay(5);
}
}
Any help would be much appreciated!
as Taurayi said but also you could clean your function up a little also like this.
function countUp(evt:MouseEvent):void
{
any_mc.gotoAndPlay(numPressed++);
if(numPressed > 5)
numPressed = 1;
}

Trying to create a function which extracts a URL from an array. JavaScript

So basically I would like to create a function that when alerted, returns the URL from an array (in this case the array is declared as 'websites'). The function has two parameters 'websites' and 'searchTerm'.
I'm struggling to make the function behave, so that when i type yahoo or google or bing in the searchTerm parameter for the function; I want it to return the corresponding URL.
Any help or support would be greatly appreciated.
Sorry if I have not made myself clear in my explanation, if this is the case, let me know and I will try and be clearer in my explanation.
Thanks in advance!
Try something more like:
var websites = {google: 'www.google.com', yahoo: 'www.yahoo.com'};
function filterURL(websites,searchTerm)
{
return websites[searchTerm] || 'www.defaultsearchwebstirehere.com';
}
** Update following comment **
Build up your websites object like so (where input is your array of key values seperated by pipe characters):
var websites = {};
for (var i = 0; i < input.length; i++) {
var siteToSearchTerm = input[i].split('|');
websites[siteToSearchTerm[1]] = siteToSearchTerm[0];
}
Here is how:
var websites = ["www.google.com|Google" , "www.yahoo.com|Yahoo" , "www.bing.com|Bing"];
function filterURL(websites,searchTerm)
{
for (var i = 0; i < websites.length; i++) {
if (websites[i].split('|')[1] === searchTerm) {
return websites[i].split('|')[0];
}
}
}
Working Example
You can also validate and improve function:
function filterURL(websites,searchTerm)
{
if (typeof websites != 'Array' || ! searchTerm) return false;
for (var i = 0; i < websites.length; i++) {
if (websites[i].split('|')[1] === searchTerm) {
return websites[i].split('|')[0];
}
}
return false;
}
Why not just use an object?
var websites = {
Google: 'www.google.com',
Yahoo: 'www.yahoo.com'
};
function filterURL(sites, searchTerm) {
if (sites[searchTerm]) {
return sites[searchTerm];
} else {
// What do you want to do when it can't be found?
}
}
alert(filterURL(websites, 'Google')); // alerts 'www.google.com'
You should really be using a hash-table like structure so that you don't have to search through the whole array every time. Something like this:
var websites = {
"Google": "www.google.com",
"Yahoo": "www.yahoo.com",
"Bing": "www.bing.com"
};
function filterURL(websites, searchTerm) {
if (websites[searchTerm] !== undefined)
return websites[searchTerm];
else
return null;
}
I'm not sure why you want to use an array for this, as what you're really doing fits a key-value pair better; however, here's how I'd do it:
function filterURL(websites, searchTerm) {
var i = 0,
parts;
for (i = 0; i < websites.length; i++) {
parts = websites[i].split("|");
if (parts[1].toLowerCase() === searchTerm) {
return parts[0];
}
}
}
But consider if you used a proper JavaScript Object instead:
var websites = {
Google: "www.google.com",
Yahoo: "www.yahoo.com",
Bing: "www.bing.com"
}
// Now it's much simpler:
function filterURL(websites, searchTerm) {
// key has first letter capitalized…
return websites[searchTerm.charAt(0).toUpperCase() + searchTerm.slice(1).toLowerCase()];
}