Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill down - Google Apps Script for Spreadsheets

Is there a function that can fill down data to all empty cells from the row above?

If not, could anyone help me out with a Google Apps Script (or other solution) that can go through every row in my google spreadsheet, check for empty cells, and if found, fill them in with data from the row above?

Such a function should be already included in a spreadsheet program I think (and "fill up" as well).

Thank you!

like image 377
Mille Avatar asked Sep 27 '12 12:09

Mille


People also ask

How do you fill down in Google spreadsheet app?

Use autofill to complete a series On your Android phone or tablet, open a spreadsheet in the Google Sheets app. In a column or row, enter text, numbers, or dates in at least two cells next to each other. To highlight your cells, drag the corner over the cells you've filled in and the cells you want to autofill.

Is there a fill tool in Google Sheets?

You can use Google Sheets autofill feature to automatically fill calculations to the bottom of a column of values as well. To do this, create the calculation you want to use in that column.


2 Answers

Such a function should be already included in a spreadsheet program I think (and "fill up" as well).

I'll attempt to describe the native functionality in GSheets pertaining to this.

  1. If you double-click the little blue square on the bottom-right of a selected cell or range, this will automatically fill that cell down to the end of a block of populated cells on the left. Alternatively, you can just drag this little blue box (down, up, left or right) to auto-fill.

  2. You can select the source cell and cells underneath it (refer to point 4), and then press Ctrl+Enter, to fill down. You can also use Ctrl+Enter or Ctrl+R to fill right.

  3. You can select the source cell, press Ctrl+C to copy, then select cells above (or to the left - and again refer to point 4), and then press Ctrl+V to paste. This has the effect of filling up (or left).

  4. You can quickly select blank cells under (or over, or right of, or left of) the source cell by pressing Ctrl+Shift+Down (or Up, or Right, or Left). This will take you to either the limit of the sheet, or the next populated cell. If the latter, you can then press Shift+Up (or Down, or Left, or Right) to back-track to the last blank cell. And then fill as required as per points 2 and 3.

  5. You will need to do this for each non-contiguous group of blank cells. If you want to be able to quickly auto-fill non-contiguous groups of blank cells, then yes, a script would be required.

like image 186
AdamL Avatar answered Sep 18 '22 09:09

AdamL


Here's a working script that adds a custom menu to your Google Sheet. Load the script for a Google Sheet, select a range on the sheet, and select "Fill Blank Cells" from the custom menu. Blank cells will be filled with the value of the cell above.

function onOpen() {   var ui = SpreadsheetApp.getUi();   // Or DocumentApp or FormApp.   ui.createMenu('Custom Menu')       .addItem('Fill Blank Cells', 'menuItem1')       .addToUi(); }  function menuItem1() {   fillBlankWithAbove() }  //Iterates over the range from top to bottom //and left to right, and fills blank cells  //with the value right above them. function fillBlankWithAbove() {   var sheet = SpreadsheetApp.getActiveSheet();   var range = sheet.getActiveRange();   Logger.log('Range height is: ' + range.getHeight());   var values = range.getValues();    var width = values[0].length;   var height = values.length;   Logger.log('Width is ' + width);   Logger.log('Height is ' + height);    for (var i = 0; i < width; i++) {     var lastVal = '';     for(var j = 0; j < height; j++) {          var currValue = values[j][i];        var dataType = typeof(currValue);       //Empty string check returns true if dataType       //is a number, and value is zero.  In that case       //we don't want to overwrite the zero, so only       //check emptyString for string types.       if(currValue === undefined ||           (dataType === 'string' && currValue == '')         )  {         //getCell parameters are relative to the current range         //and ones based          var cell = range.getCell(j+1, i+1);          cell.setValue(lastVal);        }       else {         lastVal = currValue;       }     }   }   SpreadsheetApp.flush(); } 
like image 43
lreeder Avatar answered Sep 21 '22 09:09

lreeder