skip to Main Content

I am trying to write a Google Sheets script to copy the display values from one sheet on a workbook to another sheet on a workbook. However, I am receiving the following error message, "Exception: The number of rows in the data does not match the number of rows in the range. The data has 1 but the range has 2." Here is the code

 function GEDailyData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Historical Price Data');
  var sheet2 = ss.getSheetByName('Rare Current Prices');
  var value2 = sheet2.getRange('C2').getDisplayValue();
  var value3 = sheet2.getRange('C3').getDisplayValue();
  var values = [[value2, value3]];
  var range = sheet.getRange("C2:C3");
  range.setValues(values);
}

I tried replacing the values2 and values3 variables in values with just "200" and "300", but I got the same error. Not sure where I am going wrong, but any help would be appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out. I was using the wrong syntax for the .setValues function. The correct layout for the values variable is the following:

    var values = [[value2],[value3]]; 
    

    This returns the expected result.


  2. Try this:

    function GEDailyData() {
      var ss = SpreadsheetApp.getActive();
      var sheet1 = ss.getSheetByName('Sheet1');
      var sheet2 = ss.getSheetByName('Sheet2');
      var values = sheet2.getRange('C2:C3').getDisplayValues();
      Logger.log(JSON.stringify(values));
      sheet1.getRange(2,3,values.length,values[0].length).setValues(values);
    }
    
    Execution log
    5:19:23 PM  Notice  Execution started
    5:19:23 PM  Info    [["value in c2"],["value in c3"]]
    5:19:25 PM  Notice  Execution completed
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search