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)
}
@mirfanmikra
Copy link

helpful, thankyou!

@Preeti-2311
Copy link

Preeti-2311 commented Apr 20, 2022

hey ! Thanks for this coding. You have resolved my problems of fill cell with Coding & Macros.
can you please tell me if i want to autofill the B & D also along with we're doing with A in your coding
spreadsheet.getRange("A2:A" + spreadsheet.getLastRow())
Please tell me it will help me...

@Sajidbm
Copy link

Sajidbm commented Jun 20, 2022

Thank you!!

@al-codaio
Copy link
Author

Thank you!!

You're welcome!

@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