Hi ThinhDinh
Thanks for the quick reply.
I do not know where to start with the pseudo-code XD. The reality is I have no business trying to add scripts into a google sheet (I am a landscaper) but I am ready to learn.
This is all I have in the script editor:
function ClearContentScript2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(“4.2.1 Estimate Approved Button 1st step for zap”);
var range = sheet.getRange(“A2:C100”);
range.clearContent();
}
I found this script while googling “how to clear contents while still maintaining the format”.
I need to be able to type a new estimate quickly (yep for my landscaping jobs lol). And the thing is that it was great to be able to add items to the estimate but then and I found myself into a pickle when trying to create a new one, in the app, I am taking forever to erase the old info.
I need it to be able to clear all the past data before I can write a new estimate, erasing it manually in front of the customer is a pain, so I am trying to find a way to do this “automatically” by maybe having a button that I can use to make some quick cell change, maybe a simple yes/no, or even just writing anything a letter…anything, and then (ideally) just making a simple change in a specific cell would trigger the clearing content script in a separate sheet (the sheet where I write the estimates at) and then the entire “estimates page” would be clean ready for a new estimate.
You need to setup a trigger to fire your function when an edit is made.
You could create a second function that looks something like:
function on_sheet_edit(event) {
var range = event.range;
if (range == 'range to edit to fire trigger') {
ClearContentScript2();
}
}
Then add a trigger to fire that function when an edit is made.
To add a trigger, go to the script editor and click on the icon that looks like a timer:
Then click on “Add Trigger”, and select your function to run when an event type is “On edit”. It will look like so:
I have tried the onEdit but I have encountered the same issue as when I tried the onChange trigger. it deletes it, but the problem is that it keep deleting everything while I am writing anything down. So, I thought that maybe having the trigger cell in a separate sheet may solve this problem but I do not know how to make that happen.
I have tried to use the trigger for when a google form is submitted, it only works when I did it directly from the google form (which I cannot make it work from the glide app, I tried to open the form as a link, it did not work. It did not work either when I used the glide app to submit the google form responses into its responses google sheet, the script may only be triggered when submitting the response from the google form itself, it ignores whatever changes are made directly into the response sheet).
Is anyone else now having problems with OnChange triggers working with Glide? All of a sudden mine no longer work. I thought maybe the script needed to be modified due to changes so I used Thinh’s script but still same issue. does not work unless I make the change amnually on the sheet myself. I use the form submission to add the data. The data gets added to column 35. Here is the Thinh way.
function ClearContentScript2(e) {
var ActiveSheet = e.source.getActiveSheet().getName();
var ActiveColumn = e.source.getActiveCell().getColumn();
if(ActiveSheet == ‘ADVISOR INFORMATION’ && ActiveColumn == 35 ){
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName(‘SEARCH’), true);
spreadsheet.getRange(‘W22’).clear();
spreadsheet.getRange(‘U2:U22’).clear();
spreadsheet.getRange(‘W2:W22’).clear();
spreadsheet.getRange(‘F2:F22’).clear();
spreadsheet.getRange(‘K22’).clear();
spreadsheet.getRange(‘I22’).clear();
spreadsheet.getRange(‘L22’).clear();
spreadsheet.getRange(‘N22’).clear();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName(‘MILEAGE SEARCH RESULTS’), true);
spreadsheet.getRange(‘W2:Z7’).clear();
spreadsheet.getRange(‘AM2:AN7’).clear();
}
}
And my old way:
function onChange() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == “ADVISOR INFORMATION” ) { //checks that we’re on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 35 ) { //checks the column
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === ‘’ ) //is empty?
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName(‘ADVISOR INFORMATION’), true);
spreadsheet.getRange(‘AI2:AI1000’).clear();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName(‘SEARCH’), true);
spreadsheet.getRange(‘W22’).clear();
spreadsheet.getRange(‘U2:U22’).clear();
spreadsheet.getRange(‘W2:W22’).clear();
spreadsheet.getRange(‘F2:F22’).clear();
spreadsheet.getRange(‘K22’).clear();
spreadsheet.getRange(‘I22’).clear();
spreadsheet.getRange(‘L22’).clear();
spreadsheet.getRange(‘N22’).clear();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName(‘MILEAGE SEARCH RESULTS’), true);
spreadsheet.getRange(‘W2:Z7’).clear();
spreadsheet.getRange(‘AM2:AN7’).clear();
};
}
}
I only use onChange triggers. I’m using a button with a combined action with several SetColumn actions in a sequence, with one of them changing a value in a “trigger” column. And the onchange script looks if a certain sheet has the right value in that column.
My impression is, that sometimes the onChange event is not been fired for all sheets which have changed in that sequence. Would this be possible?
I’m not sure, but I’d imagine if you add some logging to your trigger it should be possible to figure out what’s going on. eg. get it to log the sheetname every time it fires, for example.
Through the action I add values to three cells and the onChange trigger fires, and I see the correct values in the sheet, but when the onChange script reads the data in that cells, there are no values in them. Very strange!