0

So I have this script:

function copyThis() {
  const sheetName1 = 'A'
  const sheetName2 = 'B'

  sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName1)
  values = sheet1.getRange('BJ8').getValues()
  
  sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName2)
  sheet2.getRange('B12').setValues(values)
}

The result is #ERROR! in cell B12 in sheet B. Please help.

Note: Cell BJ8 contains a formula. I want to copy its value to B12 which already contains a value.

I was expecting it to get the value (a number) in cell BJ8 from sheet A and paste it into cell B12 in sheet B.

2 Answers 2

1

The problem is that your script is passing an Array to a cell. Replace

 values = sheet1.getRange('BJ8').getValues()

by

 values = sheet1.getRange('BJ8').getValue()

and

 sheet2.getRange('B12').setValues(values)

by

 sheet2.getRange('B12').setValue(values)

The above will pass a number, string, boolean or Date instead of an Array.

Resources

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

1 Comment

Thank you Ruben :) I can hardly believe that "s" at the end of getValues matters :P
0

This works also:

function copyThis() {
  const ss = SpreadsheetApp.getActive();
  const sh0 = ss.getSheetByName("Sheet0")
  const sh1 = ss.getSheetByName("Sheet1");
  sh1.getRange("A2").setValues(sh0.getRange("A1").getDisplayValues())
}

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.