Skip to content

Instantly share code, notes, and snippets.

@phillypb
Created April 1, 2022 13:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillypb/663d43a970d74c8daf896f67f48af428 to your computer and use it in GitHub Desktop.
Save phillypb/663d43a970d74c8daf896f67f48af428 to your computer and use it in GitHub Desktop.
/** * @OnlyCurrentDoc */
function autofill() {
// get active sheet
var sheet = SpreadsheetApp.getActiveSheet();
// get last row of data for later pasting into
var lastRow = sheet.getLastRow();
// create Formula for first row
var formula = "=Sum(F2+G2)";
// get source range - where Formula will be inserted and autofilled from
var sourceRange = sheet.getRange(2, 8);
// paste Formula in to first row (ignoring Header row) aka source range
sourceRange.setFormula(formula);
// get the rest of the range to fill with the Formula (includes source range). Minus 1 for Header row
var destinationRange = sheet.getRange(2, 8, lastRow - 1, 1);
// insert Formula, continuing the pattern
sourceRange.autoFill(destinationRange, SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
}
/*
This Function creates a menu item to run this script.
*/
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Admin')
.addItem('Autofill', 'autofill') // label for menu item, name of function to run.
.addToUi();
}
@p13i
Copy link

p13i commented Dec 31, 2023

tysm!

@phillypb
Copy link
Author

You're very welcome.

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