I'm trying to combine two scripts I use on google sheets into one.
They will both work on different tabs.
What is the proper way of combining scripts together?
My two scripts are as follows:
function onEdit(e) {
var sheets = ['Sanshiro', 'Yujiro', 'Mei', 'Suil', 'Martin', 'Yuta', 'Rachel','So'],
cols = [1, 6, 4],
writeCols = [15, 11],
ind = cols.indexOf(e.range.columnStart);
if (sheets.indexOf(e.source.getActiveSheet()
.getName()) === -1 || ind === -1 || !e.value) return;
if (ind === 0 && e.value === 'Update') {
e.range.setValue(new Date());
} else if (ind === 1) {
if (e.range.offset(0, 5)
.getValue() === '') e.range.offset(0, 5)
.setValue(2);
if (e.range.offset(0, 9)
.getValue() === '') e.range.offset(0, 10)
.setValue(new Date());
if (e.range.offset(0, -5)
.getValue() === '') e.range.offset(0, -5)
.setValue(new Date());
else if (ind === 1) {
if (e.range.offset(0, 1)
.getValue() === 'Updated') e.range.offset(0, 1)
.setValue(Col1);
}
}}
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Must Place candis" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2 ) { //checks the column
var nextCell = r.offset(0,12);
nextCell.setValue(new Date());
}
}
}
Any help would be much appreciated! If you can help me understand the process then I would like to learn!
Thanks so much!
How about following script? 2 script was summarized in one using "IF".
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
var sheets = ['Sanshiro', 'Yujiro', 'Mei', 'Suil', 'Martin', 'Yuta', 'Rachel','So'];
if (sheets.indexOf(e.source.getActiveSheet().getName()) > -1){
script1();
}
if(s.getName() == "Must Place candis") {
script2(s);
}
}
function script1(){
var cols = [1, 6, 4],
writeCols = [15, 11],
ind = cols.indexOf(e.range.columnStart);
if (ind === 0 && e.value === 'Update') {
e.range.setValue(new Date());
} else if (ind === 1) {
if (e.range.offset(0, 5)
.getValue() === '') e.range.offset(0, 5)
.setValue(2);
if (e.range.offset(0, 9)
.getValue() === '') e.range.offset(0, 10)
.setValue(new Date());
if (e.range.offset(0, -5)
.getValue() === '') e.range.offset(0, -5)
.setValue(new Date());
else if (ind === 1) {
if (e.range.offset(0, 1)
.getValue() === 'Updated') e.range.offset(0, 1)
.setValue(Col1);
}
}
}
function scritp2(s){
if( s.getName() == "Must Place candis" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2 ) { //checks the column
var nextCell = r.offset(0,12);
nextCell.setValue(new Date());
}
}
}
Related
On google sheets trying to hide/unhide rows based on the values in particular cells, and trying to write a script in AppsScript for that. Found that one that works in isolation (if B55=NO it hides 64 next rows):
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== 'Inputs' || range.getA1Notation() !== 'B55') return;
if (e.value === 'No') {
activeSheet.hideRows(56, 64);
} else if (e.value === 'Yes') {
activeSheet.showRows(56, 64);
}
}
But I need the same for Multiple Cells and multiple rows, as soon as I expand it only last part of the code works and not the first:
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== 'Inputs' || range.getA1Notation() !== 'B55') return;
if (e.value === 'No') {
activeSheet.hideRows(56, 64);
} else if (e.value === 'Yes') {
activeSheet.showRows(56, 64);
}
}
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== 'Inputs' || range.getA1Notation() !== 'B121') return;
if (e.value === 'No') {
activeSheet.hideRows(122, 67);
} else if (e.value === 'Yes') {
activeSheet.showRows(122, 67);
}
}
From here Cell B121 is working, but my B55 Stops working. Any tips? thanks!
You need to merge the 2 functions onEdit, try
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== 'Inputs') return;
if (range.getA1Notation() == 'B55'){
if (e.value === 'No') {
activeSheet.hideRows(56, 64);
} else if (e.value === 'Yes') {
activeSheet.showRows(56, 64);
}
}
if (range.getA1Notation() == 'B121') {
if (e.value === 'No') {
activeSheet.hideRows(122, 67);
} else if (e.value === 'Yes') {
activeSheet.showRows(122, 67);
}
}
}
Answers have already been provided, but consider the more general case:
function myFooBar() {
console.log("the first one")
}
function myFooBar() {
console.log("the second one")
}
function doMyFooBar() {
myFooBar();
}
When you run doMyFooBar(), the console shows the second one. Reading from the top of the script to the bottom, the last declaration of function myFooBar() replaces any previous declarations of that function.
Mike is correct, you can only have on onEdit() trigger, but cannot just merge as he shows since there are returns if the condition is not found
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() == 'Inputs' && range.getA1Notation() == 'B55') {;
if (e.value === 'No') {
activeSheet.hideRows(56, 64);
} else if (e.value === 'Yes') {
activeSheet.showRows(56, 64);
}
}
if (activeSheet.getName() == 'Inputs' && range.getA1Notation() == 'B121') {
if (e.value === 'No') {
activeSheet.hideRows(122, 67);
} else if (e.value === 'Yes') {
activeSheet.showRows(122, 67);
}
}
}
Creating a SharePoint Portal using JavaScript and HTML where the problem is when i'm inputting a number around 100,000 to 800,000; ex(523,546) it would enter in the first if condition and do the statement below in the. even though the value is less than the given MDV "Total Estimated Freight".
The value of the pagetdv is inputted.
var pagetdvz = document.getElementById('pagetdv');
var pagesrz = document.getElementById('pagesr');
var pagemdvz = document.getElementById('pagemdv');
/------------ Maximum Declared Value ----------/
if (document.getElementById('dropct').selectedIndex == 0)
document.getElementById('pagemdv').value = 1500000;
else if (document.getElementById('dropct').selectedIndex == 1)
document.getElementById('pagemdv').value = 3000000;
/------------ Dest + 20 Ftr ----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 50000;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 47583.67;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59981.33;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59000;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59,981.33;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 58500;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 49000;
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 51000;
/------------ Dest + 40 Ftr----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 95500;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 94944.66;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 117487.73;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 113000;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
/------------ Total Estimated Freight ----------/
if (document.getElementById('pagetdv').value > document.getElementById('pagemdv').value){
var tdva = parseFloat(pagetdvz.value) - parseFloat(pagemdvz.value);
var tdvb = tdva / 1000;
var tdvc = tdvb * 3.36;
var tef = tdvc + parseFloat(pagesrz.value);
document.getElementById('pagetef').value = tef;
}
else if (document.getElementById('pagetdv').value <= document.getElementById('pagemdv').value)
document.getElementById('pagetef').value = document.getElementById('pagesr').value;
I am trying to turn this code into a correct for loop statement, so that I can save my repetitions. I have tried my best to get it done, but I just don't know how I can write it correctly:
function myProg() {
var luckyNumber = 3;
var luckyNumber2 = 5;
var luckyNumber3 = 8;
var firstInput = document.luckForm.numberBox.value;
var secondInput = document.luckForm.numberBox2.value;
var thirdInput = document.luckForm.numberBox3.value;
var temp = '';
if (firstInput == luckyNumber && secondInput == luckyNumber2 && thirdInput == luckyNumber3 || firstInput == luckyNumber && secondInput == luckyNumber3 && thirdInput == luckyNumber2 || firstInput == luckyNumber2 && secondInput == luckyNumber3 && thirdInput == luckyNumber || firstInput == luckyNumber2 && secondInput == luckyNumber && thirdInput == luckyNumber3 || firstInput == luckyNumber3 && secondInput == luckyNumber && thirdInput == luckyNumber2 || firstInput == luckyNumber3 && secondInput == luckyNumber2 && thirdInput == luckyNumber)
{
alert('Congratulations! You got all 3 numbers correct. You\'ve won £1000!');
}
}
try something like this:
Array.prototype.getDuplicates = function() {
var cache = {}, results = [], that = this;
that.forEach(function(item, index) {
if(!cache.hasOwnProperty(item) && that.lastIndexOf(item) > index) {
results.push(item);
}
cache[item] = true;
});
return results;
}
var answers = [luckyNumber, luckyNumber2, luckyNumber3];
var indexes = [answers.indexOf(firstInput), answers.indexOf(secondInput), answers.indexOf(thirdInput)];
if(indexes.indexOf(-1) === -1 && indexes.getDuplicates().length === 0) {
// alert("Whatever");
}
Here's an example without using array. Input check was added.
function myProg() {
var numbersToMatch = 3;
var luckyNumbers = {n1: 3, n2: 5, n3: 8};
var firstInput = parseInt(document.luckForm.numberBox.value);
var secondInput = parseInt(document.luckForm.numberBox2.value);
var thirdInput = parseInt(document.luckForm.numberBox3.value);
if (isNaN(firstInput) || isNaN(secondInput) || isNaN(thirdInput)) {
alert('All inputs must be numbers!');
return;
}
var inputs = {n1: firstInput, n2: secondInput, n3: thirdInput};
var matches = {n1: false, n2: false, n3: false};
for (var i in inputs) {
for (var j in luckyNumbers) {
if ((!matches[j]) && (luckyNumbers[j] == inputs[i])) {
matches[j] = true;
numbersToMatch--;
break;
}
}
}
if (numbersToMatch == 0) {
alert('Congratulations! You got all 3 numbers correct. You\'ve won £1000!');
}
}
How can I make this more manageable? The song elements are generated by PHP so I don't know how many there will be. The number of variables for current_song is also unknown but is the same as the song elements. Thanks...
function gid(name)
{
return document.getElementById(name);
};
function itemMonitor(obj)
{
var current_song = jwplayer().getPlaylistItem().index;
gid('nowplaying').innerHTML = 'Now Playing: <span>' + player.getPlaylist()[obj.index].title + '</span>';
if (current_song == 0) {
gid('song0').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 0) {
gid('song0').style.backgroundColor = "#ffffff";}
if (current_song == 1) {
gid('song1').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 1) {
gid('song1').style.backgroundColor = "#ffffff";}
if (current_song == 2) {
gid('song2').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 2) {
gid('song2').style.backgroundColor = "#ffffff";}
if (current_song == 3) {
gid('song3').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 3) {
gid('song3').style.backgroundColor = "#ffffff";}
if (current_song == 4) {
gid('song4').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 4) {
gid('song4').style.backgroundColor = "#ffffff";}
if (current_song == 5) {
gid('song5').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 5) {
gid('song5').style.backgroundColor = "#ffffff";}
if (current_song == 6) {
gid('song6').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 6) {
gid('song6').style.backgroundColor = "#ffffff";}
if (current_song == 7) {
gid('song7').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 7) {
gid('song7').style.backgroundColor = "#ffffff";}
if (current_song == 8) {
gid('song8').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 8) {
gid('song8').style.backgroundColor = "#ffffff";}
if (current_song == 9) {
gid('song9').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 9) {
gid('song9').style.backgroundColor = "#ffffff";}
if (current_song == 10) {
gid('song10').style.backgroundColor = "#E6E8FA";}
else if (current_song !== 10) {
gid('song10').style.backgroundColor = "#ffffff";}
};
Use the following code. All song-elements are obtained through document.querySelectorAll('[id^="song"]'). Then, you loop through this collection, and set the desired property:
function gid(name) {
return document.getElementById(name);
}
function itemMonitor(obj) {
var current_song = jwplayer().getPlaylistItem().index;
var currentPlayListItem = player.getPlaylist()[obj.index];
if (!currentPlayListItem) {
// The song does not exist, atm. Do something, e.g. throw an error:
alert("Song does not exist!");
return;
}
gid('nowplaying').innerHTML = 'Now Playing: <span>' + currentPlayListItem.title + '</span>';
var all_songs = document.querySelectorAll('[id^="song"]');
for (var i=0; i<all_songs.length; i++) {
var song = all_songs[i];
var songId = /^song(\d+)$/.exec(song.id);
if (songId === null) continue; // Not a song
else songId = 1*songId[1]; // Match the songId, convert to number
all_songs[i].style.backgroundColor = current_song === songId ? "#E6E8FA" : "#FFF";
// Or, replace the previous line with:
/*if (current_song === songId) {
all_songs[i].style.backgroundColor = "#E6E8FA";
} else {
all_songs[i].style.backgroundColor = "#ffffff";
}*/
}
}
Note regarding coding style:
Function declarations (function name(){}) do not have to be postfixed by a semicolon. It's not illegal though.
if (a === b) { ... } else if (a !== b){..} can be shortened to if (a === b) {...} else { ... }, because if a is not equal to b, then it is unequal.
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() != "") {