Google Sheets Script on Form sumbission [duplicate] - google-apps-script

This question already has answers here:
Use a trigger to sort a sheet every time a form is submitted
(1 answer)
onEdit() function does not triggered when change was made by automatic script
(3 answers)
Closed last month.
I have a working script for Google sheets that checks 3 columns with trues and falses and writes baked TRUE/FALSE value for a 4th column when new data is entered.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet(); if(s.getName() == "test" ) { //checks that we're on the right sheet
var r = s.getActiveCell();
if(r.getColumn() == 1) { //checks that the cell being edited is in column A
var trgtCell = r.offset(0, 7);
if(r.offset(0, 5) == "TRUE" && r.offset(0, 6) == "TRUE" && trgtCell.getValue() === ' ') //checks the rule + if the target (trgt) cell is empty
trgtCell.setValue("TRUE");
} else {
trgtCell.setValue("FALSE");
}}}
The sheet is connected with more Google Forms.
Now my script only works when data is manually entered but when the relevant form is submitted, it creates a new row and my script does not get active. I guess because it only triggers by "edit" which I guess is manually editing the sheets.
Can anyone help me how I could convert the script to work when form is submitted? That would be my main goal.

Related

How can I use Google Sheets script to capture a user's email, name, or any information when a checkbox is selected? [duplicate]

This question already has answers here:
Unable to get Active user's email
(3 answers)
How can I get email of the last user who modified each cell
(1 answer)
Closed last month.
Purpose is a timestamp of approval - and while I can capture the date and my own email (since I'm the owner of the script), it currently captures only the date and no personal information
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if (s.getName() == "Brand Approval") { //checks that we're on the correct sheet
var r = s.getActiveCell();
var email = Session.getActiveUser().getEmail();
if (r.getColumn() == 18) { //checks the C column
var nextCell = r.offset(0, 2);
nextCell.setValue(email);
var nextCell = r.offset(0, 1);
nextCell.setValue(new Date());
}
}
}
The onEdit(e) function is a simple trigger and thus runs in a restricted context where the identity of the user at the keyboard is usually not available:
"They may or may not be able to determine the identity of the current user, depending on a complex set of security restrictions."
See Session.getActiveUser() and Session.getEffectiveUser().

Google Sheets: Hide and Unhide certain Rows when check box is checked/unchecked [duplicate]

This question already has answers here:
How to restrict onEdit to changes in specific cells?
(1 answer)
How to get the correct range to set the value to a cell?
(3 answers)
Closed 4 months ago.
As said in the title simply, I have a check box at Cell A1, I want to check it and uncheck it manually to hide a few rows in the sheets, say row 2,3. I've drafted the code below, what am i doing wrong?
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
if (sheet.getCell(1,A).isChecked() === 'TRUE');
if (e.value == "TRUE") {
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().hiderows(2, 3);
}
else if (e.value == "FALSE") {
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().showColumns(2, 3);
}
}
Error says
Error
ReferenceError: A is not defined

Having spreadsheet content move to another sheet when checkbox is clicked + dialog box show up [duplicate]

This question already has answers here:
How can I test a trigger function in GAS?
(4 answers)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I want to move a row from one spreadsheet (JobRequest) to another (Accepted) spreadsheet by clicking the checkbox automatically (no yes/no button) (located in col G). There are 7 columns of data to be moved. I want a dialog to popup at the time the box is checked too, to confirm that they have read that dialog box and accepted.
Ideally, I can log this and if they exit out of the dialog box the box unchecks, but I haven't gotten that far yet, so that's for another post.
For now, can you help be figure out where I went wrong for this checkbox to move to another sheet?
I keep getting the error
Error
TypeError: Cannot read property 'source' of undefined
I thought I already had it defined.
function onEdit(e){
const src = e.source.getActiveSpreadsheet();
const r = e.range;
if(src.getName()!= 'JobRequests' || r.columnStart !=7 || r.rowStart == 1) return;
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Accepted');
src.getRange(r.rowStart,1,1,7).moveTo(dest.getRange(dest.getLastRow()+1,1,1,7));
src.deleteRow(r.rowStart);
}
function showFeedbackDialog() {
var widget = HtmlService.createHtmlOutputFromFile("AcceptanceForm.html");
widget.setWidth(400);
widget.setHeight(500);
SpreadsheetApp.getUi().showModalDialog(widget, "Send feedback");
}
Modification points:
When I saw your question and script, the reason of your issue might be due to getActiveSpreadsheet() of const src = e.source.getActiveSpreadsheet();. e.souece returns Spreadsheet object. In this case, getActiveSpreadsheet() cannot be used. Please use getActiveSheet().
In order to open a dialog, in this modification, I used SpreadsheetApp.getUi().alert().
In order to record a log, I used other sheet.
In order to uncheck the checkbox, uncheck() is used.
When these points are reflected in your script, how about the following modification?
Modified script:
function onEdit(e) {
const src = e.source.getActiveSheet();
const r = e.range;
if (src.getName() != 'JobRequests' || r.columnStart != 7 || r.rowStart == 1 || !r.isChecked()) return;
const ui = SpreadsheetApp.getUi();
const res = ui.alert("Move row?", ui.ButtonSet.YES_NO);
if (res == ui.Button.NO) {
r.uncheck();
return;
}
const dest = e.source.getSheetByName('Accepted');
src.getRange(r.rowStart, 1, 1, 7).moveTo(dest.getRange(dest.getLastRow() + 1, 1, 1, 7));
src.deleteRow(r.rowStart);
r.uncheck();
const log = e.source.getSheetByName("log") || e.source.insertSheet("log");
log.appendRow([new Date(), `row ${r.rowStart} was moved.`]);
}
When you use this script, please check the checkbox in column "G" of "JobRequests" sheet. By this, the script is run and you can see a dialog.
And, in this modification, a log is recorded to "log" sheet. But, in this case, please modify the log data in the above script for your actual situation.
References:
Event Objects
getActiveSheet()
uncheck()

How Do I Hide and Unhide Columns Using Checkboxes and Script in Google Sheets? [duplicate]

This question already has answers here:
How to show/hide rows in Google Sheet depending on whether check box is ticked
(1 answer)
Hide/unhide rows based on checkbox on Google sheet
(2 answers)
Hide Rows Based on Check Boxes - Google App Script
(2 answers)
Hide rows based on multiple checkbox values
(1 answer)
Closed 6 months ago.
Trying to create a script that will do the following:
Only operate for one specific worksheet within my workbook
Hide multiple non-sequential columns when a checkbox is checked
Unhide multiple non-sequential columns when a checkbox is unchecked
Incorporate three checkboxes total for three separate column groups
Here is what I'd like to set up:
When the checkbox within cell A25 is checked on worksheet 'Product Pricing', columns C,E,I,K,O,Q,U,W,AA,AC,AG,AI,AM,AO,AS,AU,AY,BA,BE,BG,BK,BM will hide.
When the checkbox within cell A25 is unchecked on the same worksheet, the same columns will unhide.
Then I need to repeat the above functions with two additional column sets.
I'm not a programmer by trade but can create some pretty robust spreadsheets using scripts, functions and conditional formatting. I appreciate anyone willing to help me learn something new that I can apply to this project as well as future projects.
Thanks so much!!
It can be something like this:
function onEdit(e) {
const name = e.source.getActiveSheet().getName();
if (name != 'Sheet1') return; // if not Sheet1 do nothing
const col = e.range.columnStart;
if (col != 1) return; // if not column A do nothing
const row = e.range.rowStart;
switch (row) {
case 1: var ranges = ['d','e']; break; // checkbox on row 1
case 2: var ranges = ['f','g']; break; // checkbox on row 2
case 3: var ranges = ['h','i']; break; // checkbox on row 3
default: return;
}
const value = e.value;
if (value == 'TRUE') hide_cols(ranges); // if check --> hide
else unhide_cols(ranges); // if uncheck --> unhide
}
function hide_cols(ranges) {
var sheet = SpreadsheetApp.getActiveSheet();
ranges.forEach(r => sheet.hideColumn(sheet.getRange(r+'1')));
}
function unhide_cols(ranges) {
var sheet = SpreadsheetApp.getActiveSheet();
ranges.forEach(r => sheet.unhideColumn(sheet.getRange(r+'1')));
}
Here is my sheet.
Try this:
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Sheet0" && e.range.columnStart == 1 && e.range.rowStart == 25 && e.value == "TRUE") {
[1,5,9,11,17,21,23,27,29].forEach(c => sh.hideColumns(c));
}
if(sh.getName() == "Sheet0" && e.range.columnStart == 1 && e.range.rowStart == 25 && e.value == "FALSE") {
[1,5,9,11,17,21,23,27,29].forEach(c => sh.showColumns(c));
}
}
You will need to add the rest of the column numbers I got tired of adding them.
Also this may take longer than 30 seconds so you may require an installable onEdit trigger

i need to run Google App script in onEdit [duplicate]

This question already has an answer here:
onEdit simple trigger never seems to be triggered
(1 answer)
Closed 3 years ago.
I'm trying to create a script so that when I edit text in column A then it automatically deletes text that is in column D of the same row and if column C in the same row (it contains ID of file from my drive) is not empty then delete that file.
I successfully created it and it's working when explicitly run the script in test , but the onEdit event never seems to get fired (not seeing log messages even).
anyone have an idea how it works ?
function onEdit(e) {
var currentSheet = e.range.getSheet();
var sheetEdited = currentSheet.getName();
var rowEdited = e.range.getRow();
var columnEdited = e.range.getColumn();
// if cell edited in sheet name form 1 and in column 1 then
if(sheetEdited == "form 1" && ( columnEdited == 1)){
// delete text in edited row column D
currentSheet.getRange(rowEdited,4).setValue('');
// if on edited row column C(that contain doc id ) if filled then detele that file
if (sheetId != "") {
var sheetId = currentSheet.getRange(rowEdited,3).getValues();
DriveApp.getFileById(sheetId).setTrashed(true);
}
}
}
The problem is that simple triggers can't perform operations that require authorization. So you have to use an installable trigger. Don't forget to change the name of the function.
function onEdit(e) {
var sh = e.range.getSheet();
if(sh.getName()=="form 1" && e.range.columnStart==1){
sh.getRange(e.range.rowStart,4).setValue('');//D
var id=sh.getRange(e.range.rowStart,3).getValue();//C
if(id!="")DriveApp.getFileById(id).setTrashed(true);
}
}