.getRange().getValues() returning the wrong array.

1,019 views
Skip to first unread message

Jennifer Jones

unread,
Jan 16, 2023, 5:48:58 PM1/16/23
to Google Apps Script Community
I'm tearing my hair out here...

I'm trying to copy an array from one sheet in one spreadsheet and then paste it into a different spreadsheet and sheet.

Here is my script so far:
function sortedNewClasses() {
  var comesFromSpreadsheet = SpreadsheetApp.getActive();
  var fromSheet = comesFromSpreadsheet.getActiveSheet();
  var intosheet = masterss.getSheetByName("Copy of MasterMain9th");
  

  var newLinesNeeded = comesFromSpreadsheet.getRange("A1").getValue();
  Logger.log(newLinesNeeded);

  var arrayOfInfo = fromSheet.getRange(3,1,4,178).getValues();
  Logger.log(arrayOfInfo);

  var test = intosheet.getRange(3,1,3,4).getValues();
  Logger.log(test);

The fromsheet looks like this:
4

2127    Shaw     Samantha
2127    Shaw     Samantha
2127     Shaw     Samantha
2127     Shaw     Samantha


My 3 logger.log()s return this:
Logger.log(newLinesNeeded); //returns 0, should be 4
  Logger.log(arrayOfInfo); //returns [[#N/A, , , , , , , , , , , Smith, T, 01/17/2023, 05/26/2023, 7799-21, A,B/RP, Advisory, Y, #N/A, , 4, 4, 4, 4, 4,
//should be [[2127, Shaw, Samantha], [2127, Shaw, Samantha], [2127, Shaw, Samantha], [2127, Shaw, Samantha]

This is all truncated examples as the actual sheet has 178 columns and 24 rows.

The 3rd Logger.log returns correct information, but it is accessing the sheet I am sending the information to, not the one it is coming from.
Logger.log(test); //gives me [[2408, Allmon, Aaron, ], [2408, Allmon, Aaron, ], [2408, Allmon, Aaron, ]] which is absolutely correct.

I've tried pointing the getRange().getValues() at different cells but the result is always the same. I tried referencing the sheet I'm pulling info from with openByURL and getSheetByName() but I get the same wrong values regardless.

I've never had a problem like this before. Anyone have an idea of how to fix it? Ironically, a similar script was working just fine in the same Spreadsheet but a different sheet this morning. When I started scripting for the next sheet, it all stopped working, even the original sheet that had been working earlier.

Jennifer Jones

unread,
Jan 16, 2023, 7:06:18 PM1/16/23
to Google Apps Script Community
Inexplicably started working again. Don't know what was wrong. Didn't fix anything. But as long as it works, I'll take it.

Michael O'Shaughnessy

unread,
Jan 17, 2023, 7:06:46 PM1/17/23
to google-apps-sc...@googlegroups.com
Just my 2 cents....

I am very careful when I use "getActiveSheet" because I am not always sure I will be on the sheet that I need.  I am wondering if that is what was happening.  If the data you want is on a sheet called "theData" BUT you actually have cell A2 selected on "Sheet3", that makes "Sheet3" the active sheet.

If I know I need data from a specific tab, I will usually use getSheetByName() and pass the actual tab name so no matter what, the code will use that tab.

--
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/e31f073c-139e-49c4-91f5-f053931649ecn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages