Clear Form script with IF function for selected items

122 views
Skip to first unread message

Electrical Dept

unread,
Jul 19, 2023, 6:07:10 PM7/19/23
to Google Apps Script Community
Hey, I have the following script for an in house ordering sheet and want to be able to clear the form of the items that have been picked and the ones that are on backorder to remain. I have tried with the following script but it still clears the entire form:

function ClearCells() {
if ('E3:E50' >= true); {
var sheet = SpreadsheetApp.getActive().getSheetByName('Order Form');
sheet.getRange ('A3:A50').clearContent();}
if ('E3:E50' >= true); {
var sheet = SpreadsheetApp.getActive().getSheetByName('Order Form');
sheet.getRange ('C3:H50').clearContent();}}

To me it should only clear those that have been ticked as done (use checkboxes in google sheets).

Am I doing something wrong or just asking too much from this script? I want it to clear all except those on backorder.

💼

unread,
Jul 19, 2023, 6:22:39 PM7/19/23
to google-apps-sc...@googlegroups.com
Your “if logic” makes no sense and the entire code is duplicate, unnecessary and makes little sense. Where are you trying to read from when comparing incorrectly with >= to true?

On Jul 19, 2023, at 3:07 PM, Electrical Dept <Elect...@rvia.com.au> wrote:

Hey, I have the following script for an in house ordering sheet and want to be able to clear the form of the items that have been picked and the ones that are on backorder to remain. I have tried with the following script but it still clears the entire form:
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/cced28d4-ecf8-497b-b01c-f219b4bf0cf5n%40googlegroups.com.

Michael O'Shaughnessy

unread,
Jul 19, 2023, 6:25:00 PM7/19/23
to google-apps-sc...@googlegroups.com
Well.... I am not sure what you are trying to test in your IF statement.  You have 'E3:E50' which is a string and you are comparing it to a boolean value of "true".  I cannot think of any time this would ever be true.  So, both your IF functions are evaluating to FALSE.

Also, this function is receiving NO arguments so what are you actually testing?

Try this real quick:  right before the third line of your function add "console.log("Got here!!") and try it again.  Look at your console and see if you did infact "get here".

I think you will find you will need to work on your IF statement test.

So, you need to find the value that you need to test and send it to this function.

Also, I cannot think of any situation where I would use a test of ">=" to a boolean.  Usually, boolean's are either True or False.  (And yes, the super gurus are going to tell me that I am wrong... but for simple applications this works for me.)



Electrical Dept

unread,
Jul 19, 2023, 6:37:25 PM7/19/23
to Google Apps Script Community
Thanks for the replies. (I have done this script through google searches as I am very new to all this.)

I have this google sheets form:

Screen Shot 2023-07-20 at 8.29.40 am.png

What I want to do is hit the "Clear Form" button and it clear the form except for those marked as "Out Of Stock" so that they remain and I won't have to manually delete all the information of the picked items.

I have 2 instances of the clear script because the B column has a formula and the script deletes the formula (discovered this when I first did the script).

The "IF" function may be the wrong choice but not sure what will be the right one.

I tried using just = but it came up with errors and errors went away when I added >= (but the script still cleared the entire form).

Martin Molloy

unread,
Jul 19, 2023, 6:38:14 PM7/19/23
to google-apps-sc...@googlegroups.com
You need to loop through the rows of data and take action on each row separately.

So, there will be other ways to do this but this works 
function myFunction() {
  var range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Order Form').getRange("a1:H")
  var data = range.getValues()
  var len = data.length
  //then loop through the array
  for(var i =1; i<len-1; i++){
    var checkBox = data[i][4] // 4 if the checkbox value is in column E
    // clear the contents
    if(checkBox == true){
        data[i][0] ='' // this clears column A
        data[i][2]= '' //This clears column C
        data[i][3]= '' //This clears column D
        data[i][4]= '' //This clears column E
        data[i][5]= '' //This clears column F
        data[i][6]= '' //This clears column G
        data[i][7]= '' //This clears column H
    }
  }
  //then write the data back
  range.setValues(data)
  SpreadsheetApp.flush()
}

Hope that helps 
Martin


Electrical Dept

unread,
Jul 19, 2023, 7:03:57 PM7/19/23
to Google Apps Script Community
Thanks heaps for that. I tried that and it works great.

Electrical Dept

unread,
Jul 19, 2023, 10:09:37 PM7/19/23
to Google Apps Script Community
Update: Just realised that the script deletes the formula in column B (even though it says not to delete column B). Without that formula the sheet doesn't work properly.

Screen Shot 2023-07-20 at 12.05.07 pm.png

Martin Molloy

unread,
Jul 20, 2023, 6:56:36 AM7/20/23
to google-apps-sc...@googlegroups.com
Skipping column B

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Order Form') //
  var lastRow = sheet.getLastRow()
  var rangeAB = sheet.getRange(3,1,lastRow,2) // from row 3 Column A
  var dataAB = rangeAB.getValues()
  var rangeCH = sheet.getRange(3,3,lastRow,6)  //From row 3 column C
  var dataCH = rangeCH.getValues()
  var len = dataAB.length
  //then loop through the array
  for(var i =0; i<len-1; i++){
    var checkBox = dataCH[i][2] // 4 if the checkbox value is in column E
    // clear the contents
    if(checkBox == true){
        dataAB[i][0] ='' // this clears column A
        dataCH[i][0]= '' //This clears column C
        dataCH[i][1]= '' //This clears column D
        dataCH[i][2]= '' //This clears column E
        dataCH[i][3]= '' //This clears column F
        dataCH[i][4]= '' //This clears column G
        dataCH[i][5]= '' //This clears column H
    }
  }
  //then write the data back
  rangeAB.setValues(dataAB)
  rangeCH.setValues(dataCH)
  SpreadsheetApp.flush()
}

If you still have problems with it could you share a version of your spreadsheet - it'll be easier to spot the pitfalls

Martin

Michael O'Shaughnessy

unread,
Jul 20, 2023, 1:33:07 PM7/20/23
to google-apps-sc...@googlegroups.com
Just adding some to this....  I would suggest 2 things:
1. For column B formula use an array literal in cell B1.  Something like this: ={"Part No.", ARRAYFORMULA(IFNA(VLookup(A3:A....."}  As long as row one doesn't get deleted this will keep the formula there.
2. Use Array.filter().  After you get the data into an array filter it for the out of stock == true.  Then clear everything from row 2 to the end THEN paste the filtered values in.

I use #1 A LOT because I have built spreadsheets for others to use and they ALWAYS delete stuff from row2.  Since there is a header row MOST everyone will leave that alone. This trick saves me A LOT of time and headaches!!

Electrical Dept

unread,
Aug 2, 2023, 6:11:52 PM8/2/23
to Google Apps Script Community
Hey Martin,

Finally got a chance to change over the coding and it's doing weird things. Keeps the unchecked data but deletes the formulas in column B but keeps the data that the formula inputted there. It is also deleting the ones marked out of stock and I need to keep those in there too.

BEFORE SCRIPT:
Screen Shot 2023-08-03 at 8.06.42 am.png

AFTER SCRIPT:
Screen Shot 2023-08-03 at 8.06.54 am.png

Keith Andersen

unread,
Aug 2, 2023, 11:44:50 PM8/2/23
to google-apps-sc...@googlegroups.com
Clarification

Do you want to see only those that are picked AND out of stock or do you want to see ALL picked records and out of stock ones too?



Mark Battersby

unread,
Aug 3, 2023, 12:42:59 AM8/3/23
to google-apps-sc...@googlegroups.com

G’day,

 

Want to only see those not picked and those that are marked Out Of Stock. Ones that are marked as Picked I want to delete while keeping the formula in column 2.

 

This form is for internal parts ordering and would be more efficient to be able to clear the form of all that have been picked and leave those that are either out of stock or not picked yet.

 

Mark Battersby

RVIA Electrical Supervisor

 

From: google-apps-sc...@googlegroups.com <google-apps-sc...@googlegroups.com> on behalf of Keith Andersen <keith.a...@gmail.com>
Date: Thursday, 3 August 2023 at 1:44 pm
To: google-apps-sc...@googlegroups.com <google-apps-sc...@googlegroups.com>
Subject: Re: [Apps-Script] Clear Form script with IF function for selected items

Clarification

 

Do you want to see only those that are picked AND out of stock or do you want to see ALL picked records and out of stock ones too?

 

 

 

On Wed, Aug 2, 2023 at 5:30 PM Electrical Dept <Elect...@rvia.com.au> wrote:

Hey Martin,

 

Finally got a chance to change over the coding and it's doing weird things. Keeps the unchecked data but deletes the formulas in column B but keeps the data that the formula inputted there. It is also deleting the ones marked out of stock and I need to keep those in there too.

 

BEFORE SCRIPT:

 

AFTER SCRIPT:

 

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/rwTG2AFRTtU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAJ8MD4whA6DCCkpfQDFbC1QJWsunJ6_U42N5QAp%2B7jx4_9XNHQ%40mail.gmail.com.

Keith Andersen

unread,
Aug 3, 2023, 1:47:55 PM8/3/23
to google-apps-sc...@googlegroups.com
Try this....
function clearCells2(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Order Form');
var lastRow = sheet.getLastRow();
var startRow = 3;
var data = sheet.getRange(startRow, 1, lastRow-2, 8).getValues();
for( i = 0; i < data.length; i++){
if(data[i][4] != true || data[i][4] == "" || data[i][5] != true || data[i][5] == ""){
//sheet.getRange(i+3, 9).setValue("Yes");
sheet.getRange(i+3, 1).clearContent();
sheet.getRange(i+3, 3).clearContent();
sheet.getRange(i+3, 4).clearContent();
sheet.getRange(i+3, 5).setValue(false);
sheet.getRange(i+3, 7).clearContent();
sheet.getRange(i+3, 8).clearContent();
}//---ends if statement
}//---ends for loop
}//---ends function





Keith Andersen

unread,
Aug 3, 2023, 2:16:51 PM8/3/23
to google-apps-sc...@googlegroups.com
I actually applied it to the Copy sheet you supplied/shared. I put a yellow button "Try This" to execute the new function.

Cheers

Electrical Dept

unread,
Aug 3, 2023, 7:53:32 PM8/3/23
to Google Apps Script Community
Hey Keith,

Thanks heaps for that!

Just tried that one and works really well except I discovered that if one is marked as just "Out Of Stock" then it clears it too.

Tried a few changes to the code you wrote up but it just made things worse.

Screen Shot 2023-08-04 at 9.45.57 am.png

Screen Shot 2023-08-04 at 9.46.11 am.png

Keith Andersen

unread,
Aug 3, 2023, 7:56:50 PM8/3/23
to google-apps-sc...@googlegroups.com
So...
Picked alone - erase
Picked + Out of Stock save
Out of Stock - save

Is that to total of your criteria?

Electrical Dept

unread,
Aug 3, 2023, 8:02:10 PM8/3/23
to Google Apps Script Community
Yep, That is what is needed.

Keith Andersen

unread,
Aug 3, 2023, 8:04:37 PM8/3/23
to google-apps-sc...@googlegroups.com

Keith Andersen

unread,
Aug 3, 2023, 8:32:55 PM8/3/23
to google-apps-sc...@googlegroups.com
Try it now.

Electrical Dept

unread,
Aug 3, 2023, 9:12:51 PM8/3/23
to Google Apps Script Community
Yep, those criteria work well now...but forgot about leaving the ones that aren't picked need to remain too. Hit the button and it deletes everything except those that are marked Out Of Stock. So all the new orders placed which haven't been marked picked or out of stock are deleted too.

Screen Shot 2023-08-04 at 11.11.32 am.png

Screen Shot 2023-08-04 at 11.11.42 am.png

Keith Andersen

unread,
Aug 3, 2023, 10:46:05 PM8/3/23
to google-apps-sc...@googlegroups.com

Electrical Dept

unread,
Aug 3, 2023, 11:18:16 PM8/3/23
to Google Apps Script Community
Thank You so much guys! Works like a treat.

Keith Andersen

unread,
Aug 3, 2023, 11:20:00 PM8/3/23
to google-apps-sc...@googlegroups.com
Good deal.
Any problems crop up feel free to reach out.
Cheers
Keith 

Reply all
Reply to author
Forward
0 new messages