Enter the same value in every cell of a Range in Google Sheets using Apps Script

I recently found out that there is a very easy way to enter the same value in every cell of a Range in Google Sheets using Apps Script.

All you have to do is select the range and then use the setValue() method of the Range object.

function setValueToEveryCellInRange() {
   var range = SpreadsheetApp.getActiveSheet().getRange(1,1,5,5);
   range.setValue("Hello");
}

The above code selects the range A1:E5 of the sheet that is active (i.e., the selected sheet in your spreadsheet) and enters the text "Hello" in every cell.

Screenshot of a Google Sheets spreadsheet.

Thanks for reading!


Have feedback for me?

I'd appreciate any feedback you can give me regarding this post.

Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!