In Google Sheets I want to hide rows 5 to 31 when my drop down in R4 says BLS, and show those cells if R4 is blank or says ALS.
Here is my most recent attempt:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Shee1");
var rg = sh.getDataRange();
var vA = rg.getValues();
var R4 = sh.getRange("R4").getValue();
for (var i = 0; i < vA.length; i++) {
var row = i + 1;
switch (R4) {
case "BLS":
if (row >= 5 && row <= 31) {
sh.hideRows(row);
}
break;
case "ALS":
if (row >= 4 && row <= 32) {
sh.showRows(row);
}
break;
default:
}
}
}
I have also tried this, which points to 2 different macros that I recorded but it makes the screen go crazy and jumps around whenever something else is edited:
function onEdit(){
if(SpreadsheetApp.getActiveSheet().getRange('R4').getValue() == "BLS") BLS(); // Launches the script
if(SpreadsheetApp.getActiveSheet().getRange('R4').getValue() == "ALS") ALS(); // Launches the script
}
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Sheet1"); // Enter sheet name
var row = s.getRange('R:R').getValues();
// Enter column letter that has the text "hide" and "unhide"
s.showRows(1, s.getMaxRows());
for(var i=0; i< row.length; i++){ if(row[i] == 'hide') { s.hideRows(i+1, 27); }
else if(row[i] == 'unhide'){ s.unhideRow(ss.getDataRange()); }
}
}
Related
In sheet 'Template', I have a data validation column B and I would like to change the value in this column, if value in column AB equals X or Y. Otherwise, I want the value in column B to be the same.
I found the following script which doesn't make much sense to me
function Test1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Template");
var numRows = SpreadsheetApp.getActiveSheet().getLastRow();
var range;
/* Loop through Column AB and find cells equal to order payment
and set Column B value based on it */
for (var i = 1; i <= numRows; i++) {
range = s.getRange('AB' + i );
if (range.getValue() == "X") {
range.offset(0, 6).setValue("X");
}
else {
range.offset(0,6).setValue("NO");
}
}
}
please check if this code works for you:
function Test1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Template");
// 27 = number of columns
var sheetContent = s.getRange(1,2,s.getLastRow(),27).getValues();
for (var i = 0; i < sheetContent.length; i++) {
if(sheetContent[i][26] == "X" || sheetContent[i][26] == "Y"){
//You can replace the updated value by anything you want it to be.
sheetContent[i][0] = "Updated Value";
}
}
s.getRange(1,2,sheetContent.length,sheetContent[0].length).setValues(sheetContent);
}
Try this:
function Test1() {
const ss = SpreadsheetApp.getActive();
const sh= ss.getSheetByName("Template");
const vs = sh.getRange(1,2,sh.getLastRow(),sh.getLastColumn()).getValues()
let a = vs.map((r,i) => {
if(r[27] == "X" || r[27] == "Y") {
return ["Yes"];
} else {
return [r[1]];
}
});
sh.getRange(1,2,a.length, a[0].length).setValues(a);
}
In essence I'm trying to have a checkbox trigger addition. I have a number in Column A. When that number is entered checkboxes will appear in Column G. When that checkbox is checked I want it to trigger the addition function and add 1 to the same row in column A and not affect the other rows and then uncheck the checkbox.
So far, this is only working with Row 1 and not the other rows and I am not exactly sure why.
function onEdit (e) {
checkboxes(e);
addition(e)
}
function checkboxes(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ui = SpreadsheetApp.getUi();
var names = ss.getRange("A1:A");
var namesValues = names.getValues();
var checkboxes = ss.getRange("G1:G");
var cbRows = checkboxes.getHeight();
var cbValues = checkboxes.getValues();
var newCBValues = new Array(cbRows);
for (var row = 0; row < cbRows; row++) {
newCBValues[row] = new Array(0);
if (namesValues[row] == "" || namesValues[row] == " ") {
newCBValues[row][0] = " ";
}else{
if (cbValues[row][0] === true) {
newCBValues[row][0] = true;
}else{
newCBValues[row][0] = false;
}
}
}
checkboxes.setValues(newCBValues);
}
function addition(e) {
var ss = SpreadsheetApp.getActiveSheet();
var boxey = ss.getRange("G1:G")
var isAdd = boxey.getValue();
if (isAdd) {
Add();
boxey.setValue(false);
}
function Add() {
var ss = SpreadsheetApp.getActiveSheet();
var numbers = ss.getRange("A1:A");
numbers.setValue(numbers.getValue()+1);
}
Add one to column A of current row when checkbox in column G is checked
function onEdit(e) {
if (e.range.columnStart == 7 && e.value == "TRUE") {
const sh = e.range.getSheet();
sh.getRange(e.range.rowStart, 1).setValue(sh.getRange(e.range.rowStart, 1).getValue() + 1);
sh.getRange(e.range.rowStart, 7).setValue("FALSE")
}
checkboxes(e);//your current code
addition(e);//your current code
}
I am creating a bathroom sign out sheet for my middle school students. I have created a Google App Script that Collects students names from a dropdown box and then logs the date and time. Everything seems to work great except when 500 students have access to the sheet at the same time. If a student selects their name it can possibly change before the script can be run. I would like to replace the dropdown box with a script that will automatically grab their Google User Name. Is there anyway this can be done? I am a novice programmer and tend to get things to work by tinkering around with it, but this seems to be above my head. Here is the Google Sheet and the Script is attached as well. Thanks in advance
https://docs.google.com/spreadsheets/d/e/2PACX-1vR36FnMTbJV4Nl8X8x_VyG107Y8Q-oCSgfG3ITEvaMjSraMv-bUH2u4FuWSE74Qg1gCrO5bg12pvawe/pubhtml
function clockIn()
{
//DEFINE ALL ACTIVE SHEETS
var ss = SpreadsheetApp.getActiveSpreadsheet();
//DEFINE MAIN SHEET
var mainSheet = ss.getSheetByName("DATASHEET");
//LAST ROW ON MAIN SHEET
var lastRow = mainSheet.getLastRow();
for (var j = 5; j <= lastRow; j++)
{
// CHECK CLOCK IN
if(mainSheet.getRange('B1:B1').getValue() == mainSheet.getRange(j, 1).getValue() && mainSheet.getRange(j,3).getValue() == '')
{
Browser.msgBox("⚠️ You are Already Signed Out","Please Click the Returning to Classroom Button and then Try Again!",Browser.Buttons.OK);
return;
}
}
//_________________________________________________________________________________________________________________________________________________//
// ADD CLOCK IN RECORD
mainSheet.getRange(lastRow+1,1).setValue(mainSheet.getRange('B1:B1').getValue()).setFontSize(12);
mainSheet.getRange(lastRow+1,2).setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm:ss A/P").setHorizontalAlignment("left").setFontSize(12);
//CLEARCONTENTS
{
var sheet = ss.getSheetByName("MAIN");
sheet.getRange('B1:B2').clearContent();
}
if (mainSheet.getRange('B1:B1').getValue() == mainSheet.getRange('B1:B1').getValue() && mainSheet.getRange(j,3).getValue() == '')
{
SpreadsheetApp.getUi().alert("⏰ Please Hurry Back! ⏰","Your Teacher is Waiting",SpreadsheetApp.getUi().ButtonSet.OK);
}
}
//_________________________________________________________________________________________________________________________________________________//
function clockOut() {
//DEFINE ALL ACTIVE SHEETS
var ss = SpreadsheetApp.getActiveSpreadsheet();
//DEFINE MAIN SHEET
var mainSheet = ss.getSheetByName("DATASHEET");
//LAST ROW ON MAIN SHEET
var lastRow = mainSheet.getLastRow();
var foundRecord = false;
for (var j = 5; j <= lastRow; j++)
{
// FIND CLOCK IN RECORD
if(mainSheet.getRange('B1:B1').getValue() == mainSheet.getRange(j, 1).getValue() && mainSheet.getRange(j,3).getValue() == '')
{
SpreadsheetApp.getUi().alert("🙏 Thank You! 🙏"," " ,SpreadsheetApp.getUi().ButtonSet.OK);
// UPDATE CLOCK IN RECORD
mainSheet.getRange(j,3).setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm:ss A/P").setHorizontalAlignment("left").setFontSize(12);
var totalTime = (mainSheet.getRange(j,3).getValue() - mainSheet.getRange(j,2).getValue()) /(60*100*1000);
mainSheet.getRange(j,4).setValue(totalTime.toFixed(2)).setNumberFormat("#0.00").setHorizontalAlignment("left").setFontSize(12);
foundRecord = true;
}
}
//CLEARCONTENTS
{
var sheet = ss.getSheetByName("MAIN");
sheet.getRange('B1:B2').clearContent();
}
// IF NO CLOCK IN RECORD
if(foundRecord == false)
{
SpreadsheetApp.getUi().alert("⚠️ No Record ⚠️","You Might Have Forget to Sign Out?" ,SpreadsheetApp.getUi(). ButtonSet.OK);
}
}
//_________________________________________________________________________________________________________________________________________________//
// CALL TOTAL HOURS
TotalHours();
function TotalHours()
{
//DEFINE ALL ACTIVE SHEETS
var ss = SpreadsheetApp.getActiveSpreadsheet();
//DEFINE MAIN SHEET
var mainSheet = ss.getSheetByName("DATASHEET");
//LAST ROW ON MAIN SHEET
var lastRow = mainSheet.getLastRow();
//DEFINE ARRAY
var totals = [];
//LOOP THROUGH ALL RATES
for (var j = 5; j <= lastRow; j++)
{
var rate = mainSheet.getRange(j, 4).getValue();
var name = mainSheet.getRange(j, 1).getValue();
var foundRecord = false;
for(var i = 0; i < totals.length; i++)
{
//FOUND RECORD ADD TO TOTAL
if(name == totals[i][0] && rate != '')
{
totals[i][1] = totals[i][1] + rate;
foundRecord = true;
}
}
//ADD NEW RECORD, EXISTING RECORD NOT FOUND
if(foundRecord == false && rate != '')
{
totals.push([name, rate]);
}
}
//CLEAR DATA
mainSheet.getRange("F5:G1000").clear();
//DISPLAY TOTALS
for(var i = 0; i < totals.length; i++)
{
mainSheet.getRange(5+i,6).setValue(totals[i][0]).setFontSize(12);
mainSheet.getRange(5+i,7).setValue(totals[i][1]).setFontSize(12);
}
}
//_________________________________________________________________________________________________________________________________________________//
function ClearCells()
{
var sheet = SpreadsheetApp.getActive().getSheetByName('DATASHEET');
sheet.getRange('A5:G1000').clearContent();
}
//_________________________________________________________________________________________________________________________________________________//
function clearRange()
{
// replace 'Sheet1' with your actual sheet name
// replace 'dhrhrejYOURSHETIDerhe5j54j5j' with your actual sheet ID
var sheetActive = SpreadsheetApp.openById("1sjx0UAv73wiW-Jo1e8QWZuanjKK2tsjveVXjB1y_IHw").getSheetByName("DATASHEET");
sheetActive.getRange('A5:G1000').clearContent();
}
//_________________________________________________________________________________________________________________________________________________//
``
This works for me:
function getUserEmail() {
SpreadsheetApp.getActive().toast(Session.getActiveUser().getEmail());
}
But it may not work all of the time because of a complex set of security protocols which I know nothing about.
Since you are pertaining to the email address of the user, you can use Session.getActiveUser() which will give you the current user, then use getEmail() to get the current user's email address. Similar to what #Cooper suggested
You just need to use them in your clockIn() and clockOut():
function clockIn()
{
//DEFINE ALL ACTIVE SHEETS
var ss = SpreadsheetApp.getActiveSpreadsheet();
//DEFINE MAIN SHEET
var mainSheet = ss.getSheetByName("DATASHEET");
//LAST ROW ON MAIN SHEET
var lastRow = mainSheet.getLastRow();
for (var j = 5; j <= lastRow; j++)
{
// CHECK CLOCK IN
if(Session.getActiveUser().getEmail() == mainSheet.getRange(j, 1).getValue() && mainSheet.getRange(j,3).getValue() == '')
{
Browser.msgBox("⚠️ You are Already Signed Out","Please Click the Returning to Classroom Button and then Try Again!",Browser.Buttons.OK);
return;
}
}
//_________________________________________________________________________________________________________________________________________________//
// ADD CLOCK IN RECORD
mainSheet.getRange(lastRow+1,1).setValue(Session.getActiveUser().getEmail()).setFontSize(12);
mainSheet.getRange(lastRow+1,2).setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm:ss A/P").setHorizontalAlignment("left").setFontSize(12);
//CLEARCONTENTS
{
var sheet = ss.getSheetByName("MAIN");
sheet.getRange('B1:B2').clearContent();
}
if (Session.getActiveUser().getEmail() == Session.getActiveUser().getEmail() && mainSheet.getRange(j,3).getValue() == '')
{
SpreadsheetApp.getUi().alert("⏰ Please Hurry Back! ⏰","Your Teacher is Waiting",SpreadsheetApp.getUi().ButtonSet.OK);
}
}
//_________________________________________________________________________________________________________________________________________________//
function clockOut() {
//DEFINE ALL ACTIVE SHEETS
var ss = SpreadsheetApp.getActiveSpreadsheet();
//DEFINE MAIN SHEET
var mainSheet = ss.getSheetByName("DATASHEET");
//LAST ROW ON MAIN SHEET
var lastRow = mainSheet.getLastRow();
var foundRecord = false;
for (var j = 5; j <= lastRow; j++)
{
// FIND CLOCK IN RECORD
if(Session.getActiveUser().getEmail() == mainSheet.getRange(j, 1).getValue() && mainSheet.getRange(j,3).getValue() == '')
{
SpreadsheetApp.getUi().alert("🙏 Thank You! 🙏"," " ,SpreadsheetApp.getUi().ButtonSet.OK);
// UPDATE CLOCK IN RECORD
mainSheet.getRange(j,3).setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm:ss A/P").setHorizontalAlignment("left").setFontSize(12);
var totalTime = (mainSheet.getRange(j,3).getValue() - mainSheet.getRange(j,2).getValue()) /(60*100*1000);
mainSheet.getRange(j,4).setValue(totalTime.toFixed(2)).setNumberFormat("#0.00").setHorizontalAlignment("left").setFontSize(12);
foundRecord = true;
}
}
//CLEARCONTENTS
{
var sheet = ss.getSheetByName("MAIN");
sheet.getRange('B1:B2').clearContent();
}
// IF NO CLOCK IN RECORD
if(foundRecord == false)
{
SpreadsheetApp.getUi().alert("⚠️ No Record ⚠️","You Might Have Forget to Sign Out?" ,SpreadsheetApp.getUi(). ButtonSet.OK);
}
}
//_____
Another option is to use promt dialog to ask user to input their name when clockIn() or clockOut() was called. Then change the inputted name to capital letters.
Your Code:
function clockIn()
{
//DEFINE ALL ACTIVE SHEETS
var ss = SpreadsheetApp.getActiveSpreadsheet();
//DEFINE MAIN SHEET
var mainSheet = ss.getSheetByName("DATASHEET");
//LAST ROW ON MAIN SHEET
var lastRow = mainSheet.getLastRow();
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Enter your name:',ui.ButtonSet.OK_CANCEL);
var studentName;
// Process the user's response.
if (response.getSelectedButton() == ui.Button.OK) {
Logger.log('The user\'s name is %s.', response.getResponseText());
studentName = studentName.toUpperCase();
studentName = response.getResponseText();
} else {
Logger.log('Request was cancelled.');
return;
}
for (var j = 5; j <= lastRow; j++)
{
// CHECK CLOCK IN
if(studentName == mainSheet.getRange(j, 1).getValue() && mainSheet.getRange(j,3).getValue() == '')
{
Browser.msgBox("⚠️ You are Already Signed Out","Please Click the Returning to Classroom Button and then Try Again!",Browser.Buttons.OK);
return;
}
}
//_________________________________________________________________________________________________________________________________________________//
// ADD CLOCK IN RECORD
mainSheet.getRange(lastRow+1,1).setValue(studentName).setFontSize(12);
mainSheet.getRange(lastRow+1,2).setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm:ss A/P").setHorizontalAlignment("left").setFontSize(12);
//CLEARCONTENTS
{
var sheet = ss.getSheetByName("MAIN");
sheet.getRange('B1:B2').clearContent();
}
if (studentName == studentName && mainSheet.getRange(j,3).getValue() == '')
{
SpreadsheetApp.getUi().alert("⏰ Please Hurry Back! ⏰","Your Teacher is Waiting",SpreadsheetApp.getUi().ButtonSet.OK);
}
}
//_________________________________________________________________________________________________________________________________________________//
function clockOut() {
//DEFINE ALL ACTIVE SHEETS
var ss = SpreadsheetApp.getActiveSpreadsheet();
//DEFINE MAIN SHEET
var mainSheet = ss.getSheetByName("DATASHEET");
//LAST ROW ON MAIN SHEET
var lastRow = mainSheet.getLastRow();
var foundRecord = false;
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Enter your name:',ui.ButtonSet.OK_CANCEL);
var studentName;
// Process the user's response.
if (response.getSelectedButton() == ui.Button.OK) {
Logger.log('The user\'s name is %s.', response.getResponseText());
studentName = response.getResponseText();
studentName = studentName.toUpperCase();
} else {
Logger.log('Request was cancelled.');
return;
}
for (var j = 5; j <= lastRow; j++)
{
// FIND CLOCK IN RECORD
if(studentName == mainSheet.getRange(j, 1).getValue() && mainSheet.getRange(j,3).getValue() == '')
{
SpreadsheetApp.getUi().alert("🙏 Thank You! 🙏"," " ,SpreadsheetApp.getUi().ButtonSet.OK);
// UPDATE CLOCK IN RECORD
mainSheet.getRange(j,3).setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm:ss A/P").setHorizontalAlignment("left").setFontSize(12);
var totalTime = (mainSheet.getRange(j,3).getValue() - mainSheet.getRange(j,2).getValue()) /(60*100*1000);
mainSheet.getRange(j,4).setValue(totalTime.toFixed(2)).setNumberFormat("#0.00").setHorizontalAlignment("left").setFontSize(12);
foundRecord = true;
}
}
//CLEARCONTENTS
{
var sheet = ss.getSheetByName("MAIN");
sheet.getRange('B1:B2').clearContent();
}
// IF NO CLOCK IN RECORD
if(foundRecord == false)
{
SpreadsheetApp.getUi().alert("⚠️ No Record ⚠️","You Might Have Forget to Sign Out?" ,SpreadsheetApp.getUi(). ButtonSet.OK);
}
}
//_____
I am working on a script in which if a cell in col10 has a value it will auto resize the the row otherwise it will set the row height, in this case the row height is 72
The following image is a screenshot of what I am trying to achieve here
function reset_rows_auto() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getMaxRows().toString();
var lastRow = lastRow.replace(".0","");
for (var i = 1; i < lastRow; i++) {
var abc = sheet.getRange(i, 10) //col10
if (abc != "") {
sheet.autoResizeRows([i],1)
}
else if (abc == "") {
sheet.setRowHeight([i], 72)
}
}
}
Every time I run the script its, only auto resizing the row and not setting the row height of rows which have blank cell in Col10. I am not being able to figure the error out here, could anyone please help
Thanks in advance!
You have several typos in your code.
Try this:
function reset_rows_auto() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getMaxRows();
for (var i = 1; i < lastRow; i++) {
var abc = sheet.getRange(i, 10).getDisplayValue(); //col10
if (abc != "") {
sheet.autoResizeRows(i, 1);
}
else {
sheet.setRowHeight(i, 72);
}
}
}
I need a script triggered by a yes/no field in B2 to hide all rows in Budget_01 sheet wherever 0 is found in column B. For example in table Budget_01:
BUDGET_01 TABLE WITH HIDE ZERO [B2]= "No" All rows unhidden
Budget_01
Hide Zero|No
Expense | Amount | Frequency
Car loan 500.00 1
Gas 0 0
Music 0 1
Supplies 50 20
....
Books 0 0
BUDGET_01 TABLE WITH HIDE ZERO [B2]= "Yes" All rows where column B = $0 hidden
Budget_01
Hide Zero|Yes
Expense | Amount | Frequency
Car loan 500.00 1
Supplies 50 20
This is my attempt that does not work:
function toggleRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Budget 01");
var cellValue = sheet.getRange("B2").getValue();
var sheets = ss.getSheets();
if(cellValue == 'No'){
for(var i = 0, iLen = sheets.length; i < iLen; i++) {
// get sheet
var sh = sheets[i];
// unhide rows
var rRows = sh.getRange("A1:A");
sh.unhideRow(rRows);
}
}
if(cellValue == 'Yes'){
ss.showRows(1, ss.getMaxRows());
ss.getRange('B1:B')
.getValues()
.forEach(function (r, i) {
if (r[0] !== '' && r[0].toString()
.charAt(0) == 0) ss.hideRows(i + 1)
});
}
}
Rather than use the yes/no drop down to launch the script I inserted two graphic buttons (insert > drawing square button) marked with yes and no. I launched these two functions from each button:
function hide01Rows() {
// set up spreadsheet and sheet
var ss = SpreadsheetApp.getActiveSpreadsheet(), sheets = ss.getSheets();
for(var i = 0, iLen = sheets.length; i < iLen; i++) {
// get sheet
var sh = sheets[i];
sh.showRows(1, sh.getMaxRows());
sh.getRange('B:B')
.getValues()
.forEach(function (r, i) {
if (r[0] !== '' && r[0].toString()
.charAt(0) == 0) sh.hideRows(i + 1)
});
}
}
function unhide0Rows() {
// set up spreadsheet and sheet
var ss = SpreadsheetApp.getActiveSpreadsheet(), sheets = ss.getSheets();
for(var i = 0, iLen = sheets.length; i < iLen; i++) {
// get sheet
var sh = sheets[i];
// unhide rows
var rRows = sh.getRange("A:A");
sh.unhideRow(rRows);
}
}
This is not the most graceful solution but it does what I need.
Please change the function name of toggleRows() to onEdit(), and change B1 of "No" to "Yes". By this, when spreadsheet was edited, Budget_01--Unhidden becomes Budget_01--Hidden. If my interpretation was wrong, please tell me it.
Rather than using forEach, I used an else if statement to loop through the Yes condition.
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1"); // Change for your sheet
// Check the condition
var cellValue = sheet.getRange("B1").getValue();
// To hide/unhide, you need a range, not the value. Split into to variables.
var range = sheet.getDataRange();
var values = range.getValues();
// Check the data being returned
Logger.log(cellValue);
Logger.log(values);
// If "No," unhide the entire range.
if(cellValue == "No" || "no") {
for(var i = 0; i < values.length; i++) {
Logger.log("Unhide all rows.");
sheet.unhideRow(range);
}
// If "Yes," loop through the data looking for 0's and hide the range.
} else if (cellValue == "Yes" || "yes") {
for(var i=0; i<values.length;i++) {
if(values[i][1] == 0) {
Logger.log("Found 0 at row " + i);
sheet.hideRow(sheet.getRange(i+1,1));
}
}
}
}