1

I'm building a spreadsheet that should allow someone to add new entries but also enter past entries. I have a helper sheet that lets me code which entries are old ones that may have been edited and put them in an array. It could be 2d array (though for my tests, it's just one single row). However, setValues isn't working for various reasons. Here is the code snippet of concern:

  var newEntryStart = numOldEntries+24; //start row of new work entries
  var numNewEntries = numEntries - numOldEntries; //number of new work entries
  var oldEntries = helperSheet.getRange("N24:BQ"+numOldEntries+24).getDisplayValues();
  var newEntries = helperSheet.getRange("N"+newEntryStart+":BQ"+numNewEntries+24).getDisplayValues();
  var dbSS = SpreadsheetApp.openById("1lOjK9QJulpeUZNBcnhj_b5tXBSoTpd_LbwgcUv8VOXM");
  var dbSheet = dbSS.getSheetByName("Database");
  var testVals2 = dbSheet.getRange("A1:A").getValues();
  var firstRow = testVals2.filter(String).length+1;
  var rowList = helperSheet.getRange("D3:D").getDisplayValues();
if (numOldEntries >0 ){
  for (i=0;i<numOldEntries;i++){
    dbSheet.getRange(rowList[i],1,1,56).setValues([oldEntries[i]]);
  }
}

The error is in the setValues line at the end. I've tried various ways to turn the single row into a 2d array for setValues, but nothing works.

EDIT:
"rowList" contains the row numbers for each of the old entries in the master database, and second that each row of "oldEntries" contains 56 columns, most of which are blank, in case that is relevant to the issue I'm having. The usual error is that the number of rows doesn't match, or the number[] not valid for setValues error, things like that.

4
  • 1. Please add a minimal reproducible example 2. Instead of adding "EDIT:" at the end fix your post. Commented Jan 25, 2022 at 4:32
  • 1
    I thought that when numOldEntries is the number, numOldEntries+24 of "N24:BQ"+numOldEntries+24 is used as the string. I'm worried that this might be related to your issue. If numOldEntries is the number, when you modify "N24:BQ"+numOldEntries+24 to "N24:BQ"+(numOldEntries+24), what result will you obtain? If my guess was not useful, I apologize. Commented Jan 25, 2022 at 4:43
  • @Tanaike Thank you! That was the problem :) Please submit as an answer and I accept and upvote. To everyone else, I do apologize, I just wanted to keep it simple and included a snippet only. I don't ask a lot of questions here and, especially in my current state of frustration, I am prone to mistakes. I'll do better next time! Commented Jan 25, 2022 at 5:05
  • Thank you for replying. I'm glad your issue was resolved. From your replying, I posted it as an answer. Could you please confirm it? Commented Jan 25, 2022 at 5:09

2 Answers 2

1

When I saw your script, I thought that when numOldEntries is the number, numOldEntries+24 of "N24:BQ"+numOldEntries+24 is used as the string. I'm worried that this might be related to your issue. If numOldEntries is the number, how about the following modification?

From:

var oldEntries = helperSheet.getRange("N24:BQ"+numOldEntries+24).getDisplayValues();
var newEntries = helperSheet.getRange("N"+newEntryStart+":BQ"+numNewEntries+24).getDisplayValues();

To:

var oldEntries = helperSheet.getRange("N24:BQ"+(numOldEntries+24)).getDisplayValues();
var newEntries = helperSheet.getRange("N"+newEntryStart+":BQ"+(numNewEntries+24)).getDisplayValues();

Or, you can also use the template literal as follows.

var oldEntries = helperSheet.getRange(`N24:BQ${numOldEntries+24}`).getDisplayValues();
var newEntries = helperSheet.getRange(`N${newEntryStart}:BQ${numNewEntries+24}`).getDisplayValues();

Reference:

Sign up to request clarification or add additional context in comments.

Comments

0

Adding:

const formattedOldEntries = oldEntries.map( row => [row] );

before your for loop should give you an array containing a 2d array for each row.

Then you can use:

dbSheet.getRange(rowList[i],1,1,56).setValues(formattedOldEntries[i]);

inside your for loop and it should work. I can't say for certain though without seeing the sheet/being able to test it.

Are you trying to only update specific rows? If you are just trying to copy data from one sheet to another, you could skip the for loop and just use setValues to copy all of the data at once instead of line by line.

Documentation of Map Array Method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.