29

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!

Mille
  • 713
  • 2
  • 8
  • 10
  • 2
    Well, stackoverflow is not a place to seek readymade code. So, please try writing a script yourself using the documentation and tutorials and post a question if you encounter difficulties. – Srik Sep 27 '12 at 16:34
  • much related: http://stackoverflow.com/questions/16859941/autofill-script-google-spreadsheet-script – cregox May 20 '14 at 13:40

4 Answers4

63

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.

almcnicoll
  • 405
  • 3
  • 21
AdamL
  • 23,691
  • 6
  • 68
  • 59
  • 1
    Hi Adam, I've been working with Docs for more than two years and I didn't know all the tricks you describe here! So... thanks a lot for this useful summary:-) – Serge insas Sep 28 '12 at 05:33
  • 1
    Yeah, nice keyboard shortcuts there! But if a sheet got several hundred rows and god knows how many of those are unpopulated, then a script would be to prefer. I totally understand if no one have the time to write such a script, but I was thinking that more people would appreciate and use this. – Mille Oct 01 '12 at 19:29
  • 1
    Only dragging the bottom right handle is working for me today – Aaron McMillin Jul 24 '17 at 16:18
  • 7
    Also, you can highlight an entire column with Ctrl+Shift+Down then press Ctrl+Enter to fill the selection with whatever is in the top row. FYI (it's probably obvious) substitute Ctrl for Command on OS X. – jtpeterson Apr 06 '18 at 14:39
  • I appreciate that with a non-script answer, both Q&A are probably off-topic, but I think that this answer needs editing to include @jtpeterson's comment. Ctrl+D bookmarks the page (on Windows+Chrome at least), but Ctrl+Enter is the correct replacement. With that edit, this is the best answer. – almcnicoll Jul 02 '19 at 10:57
9

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();
}
lreeder
  • 12,047
  • 2
  • 56
  • 65
  • Thanks for this script! I've [adapted it](https://stackoverflow.com/a/66491465/4346121) to break then fill – SJGD Mar 05 '21 at 11:13
7

Use copyTo. I'll mostly adapt the answer I found about this in here:

  var sheet = SpreadsheetApp.getActiveSheet();
  var originRange = sheet.getRange("A1:B1");
  var target = sheet.getRange("A2:B");
  originRange.copyTo(target);

This will auto-fill / expand the formulas in originRange to everything below, as with copy & paste.

Community
  • 1
  • 1
cregox
  • 17,674
  • 15
  • 85
  • 116
  • 2
    Seems to me this doesn't do the same as fill down, it's just repeating the content over and over. For example a series 1,2,3 should continue not repeat. – drye Aug 15 '16 at 20:24
  • @drye so true, it will behave like copy & paste, not exactly like the fill down. A way around this is making the series just `1`, `=A1+1` and applying it to the **formula** (A2), it will expand. ;) I'm not sure if there's any other better way! :( – cregox Aug 16 '16 at 01:14
5
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Name");
var formulas = sheet.getRange(10, 1, 1, 10).getFormulasR1C1();
var newRange = sheet.getRange(11, 1, 1, 10);
newRange.setFormulasR1C1(formulas);

Using getFormulasR1C1() and then setFormulasR1C1(formulas) This function imitates mouse dragging of the formula

Please note that if the cell doesn't contain formula it will show error. You will need to work around this problem by emptying the cells.