Skip to content

Instantly share code, notes, and snippets.

@al-codaio
Last active August 5, 2024 21:34
Show Gist options
  • Save al-codaio/17a427e27077c3921695c793ad1a0440 to your computer and use it in GitHub Desktop.
Save al-codaio/17a427e27077c3921695c793ad1a0440 to your computer and use it in GitHub Desktop.
Fill cell value down until a new cell value occurs - Google Apps Script for Google Sheets
// Google Apps Script to take a value in column A and fill it down until a new value shows up in a cell for Google Sheets
// Author: Al Chen ([email protected])
// Last Updated: September 6th, 2020
// Video tutorial: https://youtu.be/t-32QkyjKVE?t=106
function fillValuesDown() {
var spreadsheet = SpreadsheetApp.getActive()
var currentRange = spreadsheet.getRange("A2:A" + spreadsheet.getLastRow())
var newRange = []
var newFillValue
currentRange.getValues().map(function(value) {
if (value[0] !== '') {
newFillValue = value[0]
newRange.push([newFillValue])
} else {
newRange.push([newFillValue])
}
})
currentRange.setValues(newRange)
}
@s-jansenidw
Copy link

s-jansenidw commented Oct 25, 2022

Very nice @al-codaio
Is there an easy way to turnaround the functionality? In my sheets I need the value from a lower cell instead of the cell above.

@al-codaio
Copy link
Author

Very nice @al-codaio Is there an easy way to turnaround the functionality? In my sheets I need the value from a lower cell instead of the cell above.

Unfortunately it would require a bit of editing to the loop that goes through all the cells in currentRange, would love for someone to figure it out!

@dedeteague
Copy link

How would this coding change if you wanted consistent text to be entered into any blank cell as the macro runs?

@al-codaio
Copy link
Author

How would this coding change if you wanted consistent text to be entered into any blank cell as the macro runs?

You could have to change up the if statement starting in row 12. You'll have to figure out a way to tell the script when to stop because it will just continue filling empty cells until the bottom of the sheet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment