COPY TO WHEN WHEN VALUE OF A8 CHANGES

560 views
Skip to first unread message

Picloan Limited

unread,
Jun 19, 2021, 12:49:43 PM6/19/21
to Google Apps Script Community
I have been trying to copy from on cell to another when another cell changes. when I run the below script, it gives TypeError: Cannot read property 'range' of undefined. Please help me.


function onChange(e){
      var s = SpreadsheetApp.getActive().getSheetByName('DATA')
  var sheet = s.range.getSheet();
  if(sheet.getName() === 'Sheet8' & e.range.getA1Notation() === "A8")  {
  
  s.getRange('E8').copyTo(s.getRange('B18'))//your script
  }

CBMServices Web

unread,
Jun 19, 2021, 12:56:28 PM6/19/21
to google-apps-sc...@googlegroups.com
The getRange  needs to be on sheet and not on s, which is the spreadsheet.

--
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/b0e5b242-4e82-464f-8eb0-471b2a9a04a4n%40googlegroups.com.

CBMServices Web

unread,
Jun 19, 2021, 12:59:10 PM6/19/21
to google-apps-sc...@googlegroups.com
Sent this too fast before adding the second statement.

S should be the spreadsheet.

So when you declare it at top, use get Active and not getActiveSheet.

Picloan Limited

unread,
Jun 19, 2021, 1:03:19 PM6/19/21
to google-apps-sc...@googlegroups.com
Thanks,but this line of code var sheet = event.range.getSheet(); still giving me this TypeError: Cannot read property 'range' of undefined

CBMServices Web

unread,
Jun 19, 2021, 1:06:54 PM6/19/21
to google-apps-sc...@googlegroups.com
Yes, you need to use getSheetName instead and then compare it against the sheet name you want to trigger from.

If it is the one you want, then getSheet can be called on s which needs to be the spreadsheet.

Picloan Limited

unread,
Jun 19, 2021, 2:34:24 PM6/19/21
to google-apps-sc...@googlegroups.com
this is what I came up with, but
 function checkValue(e) {
  var sp = PropertiesService.getScriptProperties();
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("sheet8");
  var valueToCheck = sheet.getRange("G12").getValue();
  var oldValue = sp.getProperty("G12") ||1,2,4,5 0;
  if (valueToCheck > 5 && valueToCheck != oldValue) {
   sheet.getRange('E8').copyTo(sheet.getRange('B8'))//your script
  }
}

CBMServices Web

unread,
Jun 19, 2021, 4:11:12 PM6/19/21
to google-apps-sc...@googlegroups.com
You are getting closer.

But what you need to do is: var sheetName = e.range.getSheetName()

Then compare it to the sheet name you want to monitor:

if (sheetName != " sheet8") {return;}

This way any changes to other sheets will be ignored.

Then check that the range that was changed is the one you care about:

if ((e.range.getRow() !=12) || (e.range.getColumn() != 7))
{Return;}

// now you know that your particular cell was changed. Proceed with actions you want.



cbmserv...@gmail.com

unread,
Jun 20, 2021, 12:58:21 AM6/20/21
to google-apps-sc...@googlegroups.com

Have not heard back, so I modified the function for you. I removed use of script properties because you don’t need it. Also if you want this to get triggered on edits, you need to rename your function as per below. Make sure there is only one function with that name in your script.

 

Here is what you need:

 

function onEdit(e) {

   var row = e.range.getRow();

   var col = e.range.getColumn();

    if ((row !=12) || (col != 7)) {return;}

    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var sheet = ss.getActiveSheet();

    var sheetName = sheet.getSheetName();

    if (sheetName != "Sheet8") {return;}

    var oldValue = e.oldValue;

    var valueToCheck = sheet.getRange("G12").getValue();

   if (valueToCheck > 5 && valueToCheck != oldValue) {

Picloan Limited

unread,
Jun 20, 2021, 7:55:32 AM6/20/21
to google-apps-sc...@googlegroups.com
thanks so much.you make my day

cbmserv...@gmail.com

unread,
Jun 20, 2021, 2:17:00 PM6/20/21
to google-apps-sc...@googlegroups.com

Picloan Limited

unread,
Jun 20, 2021, 4:21:09 PM6/20/21
to google-apps-sc...@googlegroups.com
please one more question on this conditional copy and paste, what if I want to copy only if cell G12 Changes like this script below


function onChange(e) {//not onEdit

   var row = e.range.getRow();

   var col = e.range.getColumn();

    if ((row !=12) || (col != 7)) {return;}

    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var sheet = ss.getActiveSheet();

    var sheetName = sheet.getSheetName();

    if (sheetName != "Sheet8"||(row.getA1Notation() !== 'G12' )  ) {return;}

Picloan Limited

unread,
Jun 20, 2021, 6:21:48 PM6/20/21
to google-apps-sc...@googlegroups.com
please ignore the first question,what I really want is ,I want to copy two cell if condition is met.




function onChange(e) {//not onEdit

   var row = e.range.getRow();

   var col = e.range.getColumn();

    if ((row !=12) || (col != 7)) {return;}

    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var sheet = ss.getActiveSheet();

    var sheetName = sheet.getSheetName();

    if (sheetName != "Sheet8"||(row.getA1Notation() !== 'G12' )  ) {return;}    

   sheet.getRange('E8').copyTo(sheet.getRange('B8'))//your script


   if (sheetName != "Sheet8"||(row.getA1Notation() !== 'D15' )  ) {return;}  

   sheet.getRange('E10').copyTo(sheet.getRange('B9'))//your script

  }

CBMServices Web

unread,
Jun 20, 2021, 8:00:58 PM6/20/21
to google-apps-sc...@googlegroups.com
If you only have the one condition to check, then once the check is done and has not returned control, you can add below it as many copies as you want.


Picloan Limited

unread,
Jun 21, 2021, 3:40:03 AM6/21/21
to google-apps-sc...@googlegroups.com
Thanks but why onChange function is not working only onEdit

CBMServices Web

unread,
Jun 21, 2021, 11:26:35 AM6/21/21
to google-apps-sc...@googlegroups.com
Not sure about that. Never personally used onChange. Perhaps someone else has experience with this.


Laurie Nason

unread,
Jun 22, 2021, 3:57:43 AM6/22/21
to google-apps-sc...@googlegroups.com
Maybe this will help explain the difference - https://itectec.com/webapp/google-sheets-diference-between-onedit-or-onchange-trigger/ 
Essentially - on edit is triggered by changing the values of cells etc - onChange is triggered when changing the structure of the sheet




--

Laurie Nason 

The KAUST School – Operational Analyst

Information Technology Support
Deep in the Bowels of the School
Landline: +966-12-808-6853

Picloan Limited

unread,
Jun 22, 2021, 6:47:38 AM6/22/21
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages