0

I'm trying to make a script for my Google spreadsheet.

What I want is for the script to check if a cell is empty in one sheet, and if its not, I want it to transfer that data into the cell of another sheet. Whenever I try this, it keeps saying "Cannot call method "getRange()" of undefined."

This is all I got so far:

function timesheets()
{
    var ss = SpreadsheetApp.openById("1iL2Didv_YOLpTad5WdR3kZSjLOLTSQk_yV-gQhh4RXs");
    if ("ss!C2" != "")
    {
      var Tasks = ss.getRange("C2").getValue();
    }
}

I'm trying to create a new function instead of using the functions on Google sheets. Would I have to include the names of both the spreadsheets?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • would ` [range.getCell(_row_,_column_)](https://developers.google.com/apps-script/reference/spreadsheet/range#getCell(Integer,Integer)) ` be what you are looking for? (eg `range.getCell(2,1)` for Row 2, Column A.) – TheDarkTurtle Aug 13 '14 at 13:14
  • Yes, that's definitely a start! – AustinWBryan Aug 13 '14 at 13:47
  • I usually use sheet.getRange(row,column), however A1 notation will work. So range.getRange(2,3) = range.getRange("C2"). Looking at .getRange and .getCell; sheet.getRange returns the Class Range relative to the sheet. range.getCell() returns the Class Range relative to the "range" which you will have to have already got. If you are getting 1 cell, they will both give you the same value back but it may be refer to a different cell on the sheet depending on what .getCell considers the "range" to be. – Niccolo Aug 13 '14 at 15:09

1 Answers1

1

You are accessing a spreadsheet, you need to get down to the sheet level first.

I suggest getting the script to work within the same spreadsheet before you try to talk from one to another.

function timesheets(){
  var ss = SpreadsheetApp.openById("1iL2Didv_YOLpTad5WdR3kZSjLOLTSQk_yV-gQhh4RXs");
  var sheet = ss.getActiveSheet();
  if("sheet!C2" != ""){
    var Tasks = sheet.getRange("C2").getValue();
  }
}

I'm trying to create a new function instead of using the functions on Google sheets. Would I have to include the names of both the spreadsheets?

The spreadsheet that has the code in it (target) needs to know the Id of the sheet that it is looking at for data (source). When you want to write the value to the (target) spreadsheet, you need to give it a variable to write to;

var sheetTarger = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

SECTION UNDERNEATH IS POST-EDIT ##

I created two spreadsheets.

  • One called source where I wrote the word "hello" into cell C2
  • In one called "target" I placed the following code

    function timesheets(){
      var ss = SpreadsheetApp.openById("*********************");
      var sheet = ss.getActiveSheet();
      if("sheet!C2" != ""){
        var Tasks = sheet.getRange("C2").getValue();
      }
      var sheetTargert = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      sheetTargert.getRange("C2").setValue(Tasks);
    }
    

I replaced "*****************" with the Id from source.

Within the script editor I ran the function timesheets()

The value "hello" appeared in the target cell C2

In the script editor I went to

View>Execution transcripts

This produced the following

[14-08-13 15:28:09:405 BST] Starting execution
[14-08-13 15:28:09:787 BST] SpreadsheetApp.openById([**************]) [0.366 seconds]
[14-08-13 15:28:09:891 BST] Spreadsheet.getActiveSheet() [0 seconds]
[14-08-13 15:28:09:975 BST] Sheet.getRange([C2]) [0.083 seconds]
[14-08-13 15:28:10:058 BST] Range.getValue() [0.082 seconds]
[14-08-13 15:28:10:058 BST] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[14-08-13 15:28:10:139 BST] Spreadsheet.getActiveSheet() [0.08 seconds]
[14-08-13 15:28:10:139 BST] Sheet.getRange([10, 10]) [0 seconds]
[14-08-13 15:28:10:140 BST] Range.setValue([hello]) [0 seconds]
[14-08-13 15:28:10:386 BST] Sheet.getRange([C2]) [0.245 seconds]
[14-08-13 15:28:10:387 BST] Range.setValue([hello]) [0 seconds]
[14-08-13 15:28:10:559 BST] Execution succeeded [0.967 seconds total runtime]
Niccolo
  • 817
  • 1
  • 10
  • 17
  • I tried that, but now nothing happens within the cell. There's no error message, but its not copying any values. – AustinWBryan Aug 13 '14 at 13:41
  • I have tested my code and edited the post to say exactly what I did. I suspect you have something like cell reference or targeting the wrong way round. – Niccolo Aug 13 '14 at 14:40
  • It keeps saying "Error, you do not have permission to cal setValue." I've been looking into it and its only supposed to show if I'm trying to change another cell's value, so I don't know why I keep getting this. I've been getting this last time too. – AustinWBryan Aug 13 '14 at 15:21
  • This looks like you are using a script in sheet1 to write to sheet2 and you or the script does not have permission to write to the other script. – Niccolo Aug 13 '14 at 15:48
  • I've been going into the sheet and using the tools>edit script button. Is that not the way I'm supposed to do it, or is there a better way? – AustinWBryan Aug 13 '14 at 15:52
  • That is correct. tools>script editor. Did you create both the spreadsheets you are using? – Niccolo Aug 13 '14 at 15:54
  • Yes, one is named Tasks and the other is named Master. I'm trying to bring the data from Tasks over to Master. Also, both sheets are on the same spreadsheet, by using the tabs at the bottom when you insert a new one. – AustinWBryan Aug 13 '14 at 16:05
  • If both sheets are on the same spreadsheet you don't need var ss = SpreadsheetApp.openById("1iL2Didv_YOLpTad5WdR3kZSjLOLTSQk_yV-gQhh4RXs"); That is for accessing one spreadsheet from another. Use var sheetTasks = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tasks"); var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master"); Or edit your post to show me your new code. – Niccolo Aug 13 '14 at 16:18
  • This is the new code I'm working with now: function timesheets(){ var sheetTasks = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tasks"); var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master"); if("sheet!C2" != ""){ var sheetTasksRange = sheetTasks.getRange("C2").getValue(); sheetMaster.setValue(sheetTasksRange); } } Its not saying I don't have permission anymore, now its just saying it can't find the setValue in object sheet. – AustinWBryan Aug 13 '14 at 19:28
  • It is the line if("sheet!C2" != ""){ . You have parenthesis round sheet!C2, this turns it into a string. Change it to if(sheet!C2 != ""){ . Also when you assign the value you have to specify where. You are using sheetMaster.setValue(sheetTasksRange). You need to set a range, e.g. sheetMaster.getRange("C2").setValue(sheetTasksRange); – Niccolo Aug 13 '14 at 22:28
  • First off, just want to let you know how much I appreciate this help so far! It told me I was missing a ) after the condition on the line with my if statement, even though I wasn't. Is this a bug? I tried to move it to just copy on cell onto another cell on the same sheet, but it gives me all kinds of errors, like it can't find the range. I haven't been able to do anything on this script and have it actually work yet. – AustinWBryan Aug 13 '14 at 23:03
  • Take a copy of the spreadsheet and script from here. https://docs.google.com/spreadsheets/d/1YtLDnuQjrCusVkrRhOhLXM2OUQVsR4jcDnjgXQtFTS4/edit?usp=sharing – Niccolo Aug 14 '14 at 07:21
  • Awesome! It works! Thanks a ton!!! Where can I learn Google Script? Is the tutorials on their site the only ones? – AustinWBryan Aug 14 '14 at 09:51
  • Glad it helped. Can you mark me as correct answer please. I learned because the script is similar to javascript (not jquery) which you can learn from many online places. I use https://developers.google.com/apps-script/reference/spreadsheet/ for scripts and https://support.google.com/docs/table/25273?hl=en for functions. The tutorials are not very expansive. – Niccolo Aug 14 '14 at 09:55
  • Right, forgot, sorry! And yeah, that's the one I using too, but I didn't finish. – AustinWBryan Aug 14 '14 at 10:48