I have a script helping me both generate a started date by a conditional column, and also moving a row to another sheet by a "Done" Column. When I use the document it works without any issues, but when others use the document the rows aren't moved to the separate sheet. Anyone got any ideas what it could be?
`
function onEdit(e) {
first(e);
second(e);
function first(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
if(
e.source.getActiveSheet().getName() == 'B2B_LeadList'
&&
col === 22
&&
e.value == 'STARTED'
&&
e.source.getActiveSheet().getRange(row,23).setValue(new Date()== ''
&&
row > 1)){
e.source.getActiveSheet().getRange(row,23).setValue(new Date());
}}
function second(e){
var mainSheet = 'B2B_LeadList'
var targetSheet = 'B2B_Archive'
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveRange();
if(sheet.getName() == 'B2B_LeadList' && e.range.getColumn() == 35 && e.range.getValue() == 'YES'){
var row = e.range.getRow();
var numColumns = sheet.getLastColumn();
var targetSheet = ss.getSheetByName('B2B_Archive');
var target = targetSheet.getRange(targetSheet.getLastRow() + 1,1);
e.source.getActiveSheet().getRange(row,36).setValue(new Date());
sheet.getRange(row,1,1,numColumns).moveTo(target);
sheet.deleteRow(row);
}
}
}
`
I am expecting that other users working in this document can move rows from current sheet to another sheet by selecting "YES" in a "Done?" column.
Try it this way:
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'B2B_LeadList' && e.range.columnStart === 22 && e.range.rowStart > 1 && e.value == 'STARTED') {
sh.getRange(e.range.rowStart, 23).setValue(new Date());
}
if (sh.getName() == 'B2B_LeadList' && e.range.columnStart == 35 && e.value == 'YES') {
var tsh = e.source.getSheetByName('B2B_Archive');
var trg = tsh.getRange(tsh.getLastRow() + 1, 1);
sh.getRange(e.range.rowStart, 36).setValue(new Date());
sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn()).moveTo(trg);
sh.deleteRow(e.range.rowStart);
}
}
If you are moving the script to their account make sure they authorize it. Otherwise ensure that they have permission to edit the file. Also note that this is a simple trigger and cannot perform functions which require permission.
I am still a newbie in this, but I want to run Onedit function script on google spreadsheet in a way that I will only get value on column E if the value on column D is "completed" and at the same time I will get value on column F depending on the value on B1 ( and that's only if the column D is"completed"). here is my spreadsheet: https://docs.google.com/spreadsheets/d/1-xLH3mb0fGngocOleCNExEmP6FnDMEFg2uLFXC10XEk/edit#gid=0
function onEdit(e){
var s = SpreadsheetApp.getActiveSpreadsheet();
if( s.getName() == "Hub" ){
var r = s.getActiveCell();
if (r.getColumn() == 4) {
var timecell = r.offSet(0, 1);
var assldapv = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Hub").getRange("B1").getValue();
var assldapo = r.offset(0, 2);
if ( r.getValue() === 'completed' ) {
timecell.setValue(new Date());
assldapo.setValue(assldapv); }
However this function is not working, so can you please give me a template on how to proceed with such script.
Try this
function onEdit(e) {
var sh = e.range.getSheet();
if (sh.getName() == "Hub" && e.range.columnStart == 4 && e.value == "completed") {
var timecell = e.range.offSet(0, 1);
var assldapv = sh.getRange("B1").getValue();
var assldapo = e.range.offset(0, 2);
timecell.setValue(new Date());
assldapo.setValue(assldapv);
}
}
I need to have 2 javascript file running on one single HTML file.
<script type="text/javascript" src="scripts/enhancements.js"></script>
<script type="text/javascript" src="scripts/part2-payment.js"></script>
Both of these script will be loaded in my payment.html page
But it is conflicting because it is loading the bottom one rather than the top one.
Script 1 - part2-payment.js
/**
* Author: Kelvin Chong Choon Siong
* Target: payment.html
* Purpose: Validate and carry forward information to Payment.html
* Created: 14 Apr 2017
* Last updated: 17-04-17
*/
"use strict";
function destroyAll(){
sessionStorage.clear();
window.location = "index.html";
}
function calcCost(bikeType, quantity){
var result = "";
if (bikeType == "Road Bicycle (City Bike) $300") {
result = (300) * quantity;
}
if (bikeType == "Cyclocross Bicycle $330") {
result = (330) * quantity;
}
if (bikeType == "Touring Bicycle $310") {
result = (310) * quantity;
}
if (bikeType == "Track/Fixed-Gear Bicycle $380") {
result = (380) * quantity;
}
if (bikeType == "Mountain Bicycle $340") {
result = (340) * quantity;
}
if (bikeType == "BMX Bicycle $380") {
result = (380) * quantity;
}
if (bikeType == "Folding Bicycle $450") {
result = (450) * quantity;
}
if (bikeType == "Tandem Bicycle $600") {
result = (600) * quantity;
}
return result;
}
function getBooking(){
if(sessionStorage.fname != undefined){ //if sessionStorage for username is not empty
//confirmation text
document.getElementById("fname").textContent = sessionStorage.fname + " " + sessionStorage.lname;
document.getElementById('address').textContent = sessionStorage.address + ", " + sessionStorage.suburb + ", " + sessionStorage.state + ", " + sessionStorage.postcode;
document.getElementById('emailadd').textContent = sessionStorage.emailadd;
document.getElementById('phonenumber').textContent = sessionStorage.pnumber;
document.getElementById('features').textContent = sessionStorage.extra;
document.getElementById('comments').textContent = sessionStorage.comments;
document.getElementById('type_bike').textContent = sessionStorage.bikeType;
document.getElementById('quantity1').textContent = sessionStorage.quantity;
document.getElementById('sizez').textContent = sessionStorage.size;
document.getElementById('cost').textContent = ("AU$ " + calcCost(sessionStorage.bikeType, sessionStorage.quantity));
//fill hidden fields
document.getElementById("firstname").value = sessionStorage.fname;
document.getElementById("lastname").value = sessionStorage.lname;
document.getElementById("emailAddress").value = sessionStorage.emailadd;
document.getElementById("homeAddress").value = sessionStorage.address;
document.getElementById("suburbs").value = sessionStorage.suburb;
document.getElementById("states").value = sessionStorage.state;
document.getElementById("postcode").value = sessionStorage.postcode;
document.getElementById("radioButtons").value = sessionStorage.radiobutton;
document.getElementById("pnumber").value = sessionStorage.pnumber;
document.getElementById("type_bike[]").value = sessionStorage.bikeType;
document.getElementById("quantity[]").value = sessionStorage.quantity;
document.getElementById("size[]").value = sessionStorage.size;
document.getElementById("costFinal").value = calcCost(sessionStorage.bikeType, sessionStorage.quantity)
document.getElementById("extraFeature").value = sessionStorage.extra;
document.getElementById("comment").value = sessionStorage.comments;
}
}
function cardExpiry(){
var errMsg = "";
var getExpiryDate = document.getElementById("expirydate").value;
var getMonth = String(getExpiryDate).charAt(0) + String(getExpiryDate).charAt(1);
var getYear = String(getExpiryDate).charAt(3) + String(getExpiryDate).charAt(4);
var dateNow = new Date ();
var expiryInput = new Date();
expiryInput.setFullYear('20' + getYear, getMonth-1, 1);
if (expiryInput < dateNow){
errMsg = "Your expiration date is before current date. Please change it.\n"
}
return errMsg;
}
function cardValidation() {
/*Visa cards have 16 digits and start with a 4
MasterCard have 16 digits and start with digits 51 through to 55
American Express has 15 digits and starts with 34 or 37.*/
var errMsg = "";
var cardNumber = document.getElementById('cardnum').value;
var cardType = document.getElementById('cardType').value;
var number0 = String(cardNumber).charAt(0);
var number1 = String(cardNumber).charAt(1);
switch (cardType) {
case "Visa":
if ((number0 !== "4") || (cardNumber.length !== 16)) {
errMsg = "Visa cards have 16 digits and start with a 4.\n";
}
break;
case "Mastercard":
if (((number0) !== "5") || ((number1) !== "1" && ((number1) !== "2") && ((number1) !== "3") && ((number1) !== "4") && ((number1) !== "5") ) || (cardNumber.length !== 16)) {
errMsg = "MasterCard have 16 digits and start with digits 51 through to 55.\n";
}
break;
case "Amex":
if (((number0) !== "3") || ((number1) !== "4" && ((number1) !== "7")) || (cardNumber.length !== 15)) {
errMsg = "Amex cards have 15 digits and start with a 34 or 37.\n";
}
break;
default:
errMsg = "Please write your correct card number.\n";
}
return errMsg;
}
function cvvValidation(){
var errMsg = "";
var cardType = document.getElementById('cardType').value;
var cvvcheck = document.getElementById('CVV').value;
//3 digits, for Visa and Mastercard, 4 digits for American Express.
switch (cardType) {
case "Visa":
if (cvvcheck.length !== 3) {
errMsg = "Visa cards have a CVV of 3 digits.\n";
}
break;
case "Mastercard":
if (cvvcheck.length !== 3) {
errMsg = "MasterCards have a CVV of 3 digits.\n";
}
break;
case "Amex":
if (cvvcheck.length !== 4) {
errMsg = "Amex cards have a CVV of 4 digits..\n";
}
break;
default:
errMsg = "Please write your correct card CVV.\n";
}
return errMsg;
}
function validate() {
var errMsg = "";
var result = true;
var checkCard = cardValidation();
if (checkCard !== "") {
errMsg = errMsg + checkCard;
result = false ;
}
var checkCVV = cvvValidation();
if (checkCVV !== "") {
errMsg = errMsg + checkCVV;
result = false ;
}
var checkExpiry = cardExpiry();
if (checkExpiry !== ""){
errMsg = errMsg + checkExpiry;
result = false;
}
if (errMsg != "") {
alert(errMsg);
}
return result;
}
function init() {
getBooking();
var regForm = document.getElementById("form2");
regForm.onsubmit= validate;
var destroyEverything = document.getElementById('cancelout');
destroyEverything.onclick = destroyAll;
}
window.onload = init;
So this is Script 2 now
Script 2 - enhancements.js
"use strict";
function writeNewMessage(){
var sMessage = document.getElementById('spanny');
sMessage.textContent = "this is a test";
}
function init2() {
var labelTest = document.getElementById("booking_form");
labelTest.onclick = writeNewMessage;
}
window.onload = init2;
I can't figure out how to load both in at the same time.
A few restrictions that I have (because most of the solutions provided on the net I can't use)
body onload
No Inline javascript
No jQuery
So I was wondering if there is another one to make both work?
Your onloads are overwriting each other:
window.onload = init2;//will override window.onload=init;
So do:
window.addEventListener("load",init2);
Same for all the other clicks, etc.
Managed to figure it out by using
window.onload = function() {
init();
init2();
};
doing this on either one of the js script would work.
All good. On n'est jamais mieux servi que par soi-meme.
I am new to JavaScript, I have been learning and practicing for about 3 months and hope I can get some help on this topic. I'm making a poker game and what I'm trying to do is determine whether i have a pair, two pairs, three of a kind, four of a kind or a full house.
For instance, in [1, 2, 3, 4, 4, 4, 3], 1 appears one time, 4 appears three times, and so on.
How could I possibly ask my computer to tell me how many times an array element appears?
Solved, here's the final product.
<script type="text/javascript">
var deck = [];
var cards = [];
var convertedcards = [];
var kinds = [];
var phase = 1;
var displaycard = [];
var options = 0;
var endgame = false;
// Fill Deck //
for(i = 0; i < 52; i++){
deck[deck.length] = i;
}
// Distribute Cards //
for(i = 0; i < 7; i++){
cards[cards.length] = Number(Math.floor(Math.random() * 52));
if(deck.indexOf(cards[cards.length - 1]) === -1){
cards.splice(cards.length - 1, cards.length);
i = i - 1;
}else{
deck[cards[cards.length - 1]] = "|";
}
}
// Convert Cards //
for(i = 0; i < 7; i++){
convertedcards[i] = (cards[i] % 13) + 1;
}
// Cards Kind //
for(i = 0; i < 7; i++){
if(cards[i] < 13){
kinds[kinds.length] = "H";
}else if(cards[i] < 27 && cards[i] > 12){
kinds[kinds.length] = "C";
}else if(cards[i] < 40 && cards[i] > 26){
kinds[kinds.length] = "D";
}else{
kinds[kinds.length] = "S";
}
}
// Card Display //
for(i = 0; i < 7; i++){
displaycard[i] = convertedcards[i] + kinds[i];
}
// Hand Strenght //
var handstrenght = function(){
var usedcards = [];
var count = 0;
var pairs = [];
for(i = 0, a = 1; i < 7; a++){
if(convertedcards[i] === convertedcards[a] && a < 7 && usedcards[i] != "|"){
pairs[pairs.length] = convertedcards[i];
usedcards[a] = "|";
}else if(a > 6){
i = i + 1;
a = i;
}
}
// Flush >.< //
var flush = false;
for(i = 0, a = 1; i < 7; i++, a++){
if(kinds[i] === kinds[a] && kinds[i] != undefined){
count++;
if(a >= 6 && count >= 5){
flush = true;
count = 0;
}else if(a >= 6 && count < 5){
count = 0;
}
}
}
// Straight >.< //
var straight = false;
convertedcards = convertedcards.sort(function(a,b){return a-b});
if(convertedcards[2] > 10 && convertedcards[3] > 10 && convertedcards[4] > 10){
convertedcards[0] = 14;
convertedcards = convertedcards.sort(function(a,b){return a-b});
}
alert(convertedcards);
if(convertedcards[0] + 1 === convertedcards[1] && convertedcards[1] + 1 === convertedcards[2] && convertedcards[2] + 1 === convertedcards[3] && convertedcards[3] + 1 === convertedcards[4]){
straight = true;
}else if(convertedcards[1] + 1 === convertedcards[2] && convertedcards[2] + 1 === convertedcards[3] && convertedcards[3] + 1 === convertedcards[4] && convertedcards[4] + 1 === convertedcards[5]){
straight = true;
}else if(convertedcards[2] + 1 === convertedcards[3] && convertedcards[3] + 1 === convertedcards[4] && convertedcards[4] + 1 === convertedcards[5] && convertedcards[5] + 1 === convertedcards[6]){
straight = true;
}
// Royal Flush, Straight Flush, Flush, Straight >.< //
var royalflush = false;
if(straight === true && flush === true && convertedcards[6] === 14){
royalflush = true;
alert("You have a Royal Flush");
}
else if(straight === true && flush === true && royalflush === false){
alert("You have a straight flush");
}else if(straight === true && flush === false){
alert("You have a straight");
}else if(straight === false && flush === true){
alert("You have a flush");
}
// Full House >.< //
if(pairs[0] === pairs[1] && pairs[1] != pairs[2] && pairs.length >= 3){
fullhouse = true;
alert("You have a fullhouse");
}else if(pairs[0] != pairs[1] && pairs[1] === pairs[2] && pairs.length >= 3){
fullhouse = true;
alert("You have a fullhouse");
}else if(pairs[0] != pairs[1] && pairs[1] != pairs[2] && pairs[2] === pairs[3] && pairs.length >= 3){
fullhouse = true;
alert("You have a fullhouse");
}
// Four of a kind >.< //
else if(pairs[0] === pairs[1] && pairs[1] === pairs[2] && pairs.length > 0){
alert("You have four of a kind");
}
// Three of a kind >.< //
else if(pairs[0] === pairs[1] && flush === false && straight === false && pairs.length === 2){
alert("You have three of a kind");
}
// Double Pair >.< //
else if(pairs[0] != pairs[1] && flush === false && straight === false && pairs.length > 1){
alert("You have a double pair");
}
// Pair >.< //
else if(pairs.length === 1 && flush === false && straight === false && pairs.length === 1 ){
alert("You have a pair");
}
alert(pairs);
};
while(endgame === false){
if(phase === 1){
options = Number(prompt("Your hand: " + displaycard[0] + " " + displaycard[1] + "\n\n" + "1. Check" + "\n" + "2. Fold"));
}else if(phase === 2){
options = Number(prompt("Your hand: " + displaycard[0] + " " + displaycard[1] + "\n\n" + displaycard[2] + " " + displaycard[3] + " " + displaycard[4] + "\n\n" + "1. Check" + "\n" + "2. Fold"));
}else if(phase === 3){
options = Number(prompt("Your hand: " + displaycard[0] + " " + displaycard[1] + "\n\n" + displaycard[2] + " " + displaycard[3] + " " + displaycard[4] + " " + displaycard[5] + "\n\n" + "1. Check" + "\n" + "2. Fold"));
}else if(phase === 4){
options = Number(prompt("Your hand: " + displaycard[0] + " " + displaycard[1] + "\n\n" + displaycard[2] + " " + displaycard[3] + " " + displaycard[4] + " " + displaycard[5] + " " + displaycard[6] + "\n\n" + "1. Check" + "\n" + "2. Fold"));
}
switch(options){
case 1:
if(phase === 5){
handstrenght();
endgame = true;
}else{
phase++;
}
break;
case 2:
endgame = true;
break;
default:
endgame = true;
break;
}
}
</script>
Keep a variable for the total count
Loop through the array and check if current value is the same as the one you're looking for, if it is, increment the total count by one
After the loop, total count contains the number of times the number you were looking for is in the array
Show your code and we can help you figure out where it went wrong
Here's a simple implementation (since you don't have the code that didn't work)
var list = [2, 1, 4, 2, 1, 1, 4, 5];
function countInArray(array, what) {
var count = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] === what) {
count++;
}
}
return count;
}
countInArray(list, 2); // returns 2
countInArray(list, 1); // returns 3
countInArray could also have been implemented as
function countInArray(array, what) {
return array.filter(item => item == what).length;
}
More elegant, but maybe not as performant since it has to create a new array.
With filter and length it seems simple but there is a big waste of memory.
If you want to use nice array methods, the appropriate one is reduce:
function countInArray(array, value) {
return array.reduce((n, x) => n + (x === value), 0);
}
console.log(countInArray([1,2,3,4,4,4,3], 4)); // 3
Well..
var a = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4].reduce(function (acc, curr) {
if (typeof acc[curr] == 'undefined') {
acc[curr] = 1;
} else {
acc[curr] += 1;
}
return acc;
}, {});
// a == {2: 5, 4: 1, 5: 3, 9: 1}
from here:
Counting the occurrences of JavaScript array elements
Or you can find other solutions there, too..
When targeting recent enough browsers, you can use filter(). (The MDN page also provides a polyfill for the function.)
var items = [1, 2, 3, 4, 4, 4, 3];
var fours = items.filter(function(it) {return it === 4;});
var result = fours.length;
You can even abstract over the filtering function as this:
// Creates a new function that returns true if the parameter passed to it is
// equal to `x`
function equal_func(x) {
return function(it) {
return it === x;
}
}
//...
var result = items.filter(equal_func(4)).length;
Here's an implementation of Juan's answer:
function count( list, x ) {
for ( var l = list.length, c = 0; l--; ) {
if ( list[ l ] === x ) {
c++;
}
}
return c;
}
Even shorter:
function count( list, x ) {
for ( var l = list.length, c = 0; l--; list[ l ] === x && c++ );
return c;
}
Here's an implementation that uses the Array Object Prototype and has an extra level of functionality that returns the length if no search-item is supplied:
Array.prototype.count = function(lit = false) {
if ( !lit ) { return this.length}
else {
var count = 0;
for ( var i=0; i < this.length; i++ ) {
if ( lit == this[i] ){
count++
}
}
return count;
}
}
This has an extremely simple useage, and is as follows:
var count = [1,2,3,4,4].count(4); // Returns 2
var count = [1,2,3,4,4].count(); // Without first parameter returns 5
I was told I should consolidate my if statements. I'm not sure how to do this? Also, is there anything else wrong in this script? It is for a google doc script.
function onEdit(e) {
var colorA = "yellow";
var colorB = "#dddddd";
var colorC = "#dddddd";
var sheet = e.source.getActiveSheet();
var range = e.source.getActiveRange();
// 3 is column C
if (range.getColumn() == 3) {
if (range.getValue() != "") {
sheet.insertRowAfter(range.getRow());
var r = range.getRow() + 1;
sheet.getRange("A" + r + ":H" + r).setBackgroundColor(colorC);
}
}
if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {
var rows = sheet.getMaxRows();
//two ranges
//column C
var rangeC = sheet.getRange("C1:C"+rows);
var valuesC = rangeC.getValues();
//column H range
var rangeH = sheet.getRange("H1:H"+rows);
var colorH = rangeH.getBackgroundColors();
var valuesH = rangeH.getValues();
//iterate over each row in column C and H
//then change color
for (var row = 0; row < valuesC.length; row++) {
//check for columnC and column H
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}
}
sheet.getRange("H1:H" + rows).setBackgroundColors(colorH);
}
}
Here is the other one
ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "New PO", functionName: "NewPO"}];
ss.addMenu("New PO", menuEntries);
}
function NewPO() {
SpreadsheetApp.getActiveSheet().insertRowsBefore(1,6);
// Adjust this range accordingly, these are the cells that will be
// copied. Format is getRange(startRow, startCol, numRows, numCols)
ss.getSheetByName("PO Form").getRange(1, 1, 6, 8)
.copyTo(SpreadsheetApp.getActiveSheet().getRange(1, 1, 6, 8));
}
function onEdit(e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
// 1 is A, 2 is B, ... 8 is H
if (r.getColumn() == 8 && r.getValue() == "x") {
r.setValue(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"));
}
}
Besides what murray noted, there are several instances where you repeat the same expression:
if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {
could be:
var col = e.source.getActiveRange().getColumn();
if(col == 3 || col == 8) {
This applies to a lesser extent to:
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}
which could be (for instance):
var hRow = colorH[row];
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
hRow[0] = colorA;
} else if (valuesH[row][0] != "") {
hRow[0] = colorB;
}
only thing i can see:
// 3 is column C
if (range.getColumn() == 3) {
if (range.getValue() != "") {
// 3 is column C
if (range.getColumn() == 3 && range.getValue() != "") {