script: onEdit on specific ranges and sheets

2,503 views
Skip to first unread message

Julien PANEVEL

unread,
Jul 29, 2022, 10:27:09 PM7/29/22
to Google Apps Script Community
Hi there,

I'm looking for a script to put borders on empty cells if a modification takes place.

And this, on specific ranges (ex: 'A1:C4' and 'E1:G4') then on sheets whose name contains the letter "s".

I started this code:

function onEdit(e) {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();

   if (e.range.columnStart >= 1 &&
       e.range.columnEnd <= 3 &&
       e.range.rowStart >= 1 &&
       e.range.rowEnd <= 4)
       { range.setBorder(true, true, true, true, true, true,)};
  }

I'm blocked from the start because it returns me range is underfined.

I hope you can help me.

Good for you.

CBMServices Web

unread,
Jul 29, 2022, 11:21:05 PM7/29/22
to google-apps-sc...@googlegroups.com
The variable range used inside the if statement has not been defined.

You need to define what it is before using it.

Did you mean to use e.range?

--
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/5b85c160-4e30-4e23-b874-727e6a775062n%40googlegroups.com.

Julien PANEVEL

unread,
Jul 30, 2022, 9:53:25 AM7/30/22
to Google Apps Script Community
Thank you George !

silly! I was almost there XD

Can you help me by telling me what code to put if I want to include in the conditions that the onEdit() function only applies to sheets that contain "week" in their title?

CBMServices Web

unread,
Jul 30, 2022, 12:30:00 PM7/30/22
to google-apps-sc...@googlegroups.com
Hi Julien,

No worries, we have all been there. Sometimes we stare at something for too long we don't see the syntaxes error.

To check the sheet name, just grab the name of the sheet by doing:

var sheetName = sheet.getName();

Then check if it contains week.

if (sheetName.indexOf("week") < 0)
{
  // Not right sheet
 return;
}


Julien PANEVEL

unread,
Jul 30, 2022, 4:33:09 PM7/30/22
to google-apps-sc...@googlegroups.com
Hello :)

Cool, thank you.

I found another way :

if( sheet.getName().includes('semaine') )

with, before :

var sheet = e.source.getActiveSheet();



--

          UCPA

 Julien PANEVEL

Directeur
Centre Aquatique Communautaire
UNITÉ SPORT LOISIRS

Tel :   05 96 76 58 83
Fax : 05 96 76 58 89
Petit Manoir - 97232 Le Lamentin -  Martinique

Site Internet    Facebook    Twitter    YouTube    WeAreUcpa

Sport your nature

Protégeons l’environnement, n’imprimons ce document que si nécessaire


Reply all
Reply to author
Forward
0 new messages