I'm trying to make a google spreadsheet script.
When the value of a cell changes to a specified value I want it to trigger hide/unhide rows.
Here is my script so far:
function autoHide() {
var ss = SpreadsheetApp.getActive()
var sheet = SpreadsheetApp.getActiveSheet()
var cell = ss.getActiveCell()
var cell1 = ("C12");
if (cell1 == "Others" ) {
ss.getActiveSheet().showRows(14, 3);
}
else if (cell1 == "Additional Discount/s (over and above 25%)" ) {
ss.getActiveSheet().showRows(17, 4);
}
else {
ss.getActiveSheet().hideRows(14, 7)
}
}
Your question is a little unclear about some points so I assumed you wanted to track the value of cell C12 when C12 is edited.
function autoHide() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell();
var value = cell.getValue();
var cellRef = cell.getA1Notation();
if (cellRef !== "C12") return;
// This isn't the cell we are looking for, stop executing the function.
if (value === "Others" ) {
sheet.showRows(14, 3);
} else if (value === "Additional Discount/s (over and above 25%)" ) {
sheet.showRows(17, 4);
} else {
sheet.hideRows(14, 7);
}
}
If you want to run the function anytime any cell is edited, use this code.
Useful if C12 is a formula and is not updated manually.
function autoHide() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var cellRef = "C12";
var cell = sheet.getRange(cellRef);
var value = cell.getValue();
if (value === "Others" ) {
sheet.showRows(14, 3);
} else if (value === "Additional Discount/s (over and above 25%)" ) {
sheet.showRows(17, 4);
} else {
sheet.hideRows(14, 7);
}
}
Related
I have the below code that works a treat on one sheet for selecting multiple options into one cell from a dropdown list, but I am borrowing code and I am not a code monkey so is there a way to do this function and apply it to multiple sheets?
Its the same column across the multiple sheets
function onEdit(e) {
var oldValue;
var newValue;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeCell = ss.getActiveCell();
if (activeCell.getColumn() == 3 && activeCell.getRow() == 1 && ss.getActiveSheet().getName() == "Sheet1") {
newValue = e.value;
oldValue = e.oldValue;
if (!e.value) {
activeCell.setValue("");
} else {
if (!e.oldValue) {
activeCell.setValue(newValue);
} else {
if (oldValue.indexOf(newValue) < 0) {
activeCell.setValue(oldValue + ',' + newValue);
} else {
activeCell.setValue(oldValue);
}
}
}
}
}
Any help would be greatly appreciated.
Thanks
Remove && ss.getActiveSheet().getName() == "Sheet1". It's a condition that makes the code work only in the sheet names Sheet1.
I am attempting to create a additional menu option that allow me to automatically delete all rows that contain the text "Complete" in Column D. The script I have runs and finishes with no errors but does not delete the rows. I tried to find help on line and tweaked the code but same result
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 'Complete';
for (var i = 'Complete'; i <= numRows - 1; i++) {
var row = values[i];
if (row[2] == 'Complete' || row[2] == '') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Delete Rows where column shows Complete Text",
functionName : "readRows"
}];
sheet.addMenu("Script Center Menu", entries);
};
I have an automated time stamp script that works when clicking a checkbox, a time stamp is populated in the cell next to it. How do I make my script work on certain rows?
Example: Column C has check boxes, Column D is populated with dates when Check boxes are marked.
What I want my TimeStamp script to do:
Work only on Row 11, all the way down.
var COLUMNTOCHECK = 3;
var DATETIMELOCATION = [0, 1];
var SHEETNAME = 'Training Dash'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
if (selectedCell.getColumn() >= COLUMNTOCHECK && selectedCell.getColumn() % 2 == 1) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}
if I understood correctly, this should help.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
if (selectedCell.getColumn() >= COLUMNTOCHECK && selectedCell.getColumn() %
2 == 1 && selectedCell.getRow() > 10) {
var dateTimeCell =
selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}
follow script is used to get a date from a google sheet and if this date is equal to today or tomorrow generate an automatic email to my address in order to remind me.
function getVal() {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
sheet = ss.getActiveSheet();
sheet.setActiveRange(sheet.getRange("A1"));
var range = sheet.getDataRange(),
formulas = range.getValues();
var toDay = new Date();
for (var r=0; r<formulas.length; r++) {
for (var c=0; c<formulas[r].length; c++) {
//var value = sheet.getRange(r,c).getValue();
var value = range.getCell(r, c).getValue();
Logger.log(value);
if (value == "AAAA")
{
var index = r+2;
value = sheet.getRange(index,c).getValue();
while (value != "" || index >= formulas.length)
{
if (DateDiff.inDays(value,toDay)==1 || DateDiff.inDays(value,toDay)==0)
{
MailApp.sendEmail(myAdress,subject, text);
}
index = index + 3;
value = sheet.getRange(index,c).getValue();
}
}
}
}
}
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M+12*d2Y)-(d1M+12*d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
when i run the script it fail in the line :
var value = range.getCell(r, c).getValue();
have you any suggestion in order to fix this bug?
thanks
Mario
try to change the line with:
var value = formulas[r][c]
it should also do less request to google spreadsheet and run faster.
here the file
https://docs.google.com/spreadsheet/ccc?key=0AurtxTJwggIpdG9GaE1TOV9wMEw2UHhjZVJyUXVETUE
you'll find on sheet Key, what is meant to do :
Find in all sheets, first in column B the row where the reference (Key!B1) stands, then starting from this row, find in column C where the reference (Key!B2) stands for first
in other words, we are looking for the couple and make the cell in column C as activecell
the script is working in debug mode, only
running it using the button in sheet Key, make it "not select" the good cell, even if the cell to be selected is found (logged in Logger)
I use a color function
That mean, blue or red is the cell to be selected , and sometimes it's not selected
the code :
function lookFor() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var referSheet=ss.getSheetByName("Key");
var sheetToCheck = new Array;
sheetToCheck[0]=ss.getSheetByName("work");
sheetToCheck[1]=ss.getSheetByName("user");
sheetToCheck[2]=ss.getSheetByName("tax");
sheetToCheck[3]=ss.getSheetByName("cont");
sheetToCheck[4]=ss.getSheetByName("ind");
var referenceB=referSheet.getRange("B1").getValue();
var referenceC=referSheet.getRange("B2").getValue();
//loop to work with all reference as long as there are data in the column A
for (j in sheetToCheck){
sheetToCheck[j].setActiveCell("A1");
var dataToCheck=new Array;
dataToCheck=sheetToCheck[j].getRange(1,2,sheetToCheck[j].getLastRow(),2).getValues();
for (i in dataToCheck){
var done=false;
if (dataToCheck[i][0]==referenceB){
for (k=i;k<dataToCheck.length;k++){
if (dataToCheck[k][1]==referenceC){
//this part is user to change the color of the cell, to check if the code is working well
if (sheetToCheck[j].getRange(parseInt(k)+1,3,1,1).getBackgroundColor()=="red"){
sheetToCheck[j].getRange(parseInt(k)+1,3,1,1).setBackgroundColor("blue");
}
else
{
sheetToCheck[j].getRange(parseInt(k)+1,3,1,1).setBackgroundColor("red");
}
var cell=sheetToCheck[j].getRange(parseInt(k)+1,3,1,1).getA1Notation() ;
sheetToCheck[j].setActiveCell(cell);
SpreadsheetApp.flush();
done=true;
Logger.log("Sheet :"+sheetToCheck[j].getName()+" - Cell :"+cell);
break;
}
}
if (done==true){
break;
}
}
}
}
}
enter code here
I noticed you aready fixed this in your script:
var referenceB=referSheet.getRange("B1").getValue(); //A2
var referenceC=referSheet.getRange("B2").getValue(); //B2
Here is a shorter version of your code :)
function LookForCity() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var referSheet = ss.getSheetByName("Key").getDataRange().getValues()[1];
var sheetName = ["work", "user", "tax", "cont", "ind"];
for (j in sheetName){
var sheet = ss.getSheetByName(sheetName[j]);
var values = sheet.getDataRange().getValues();
for(i in values){
if(values[i][1] == referSheet[0] && values[i][2] == referSheet[1]){
Logger.log('Sheet: ' + sheetName[j]+ ' Cell: C'+(parseInt(i)+1) );
sheet.setActiveCell(sheet.getRange(parseInt(i)+1, 3));
Utilities.sleep(3000);
break;
}
}
}
}