简体   繁体   中英

AutoFill Data with Blank Rows - Google Sheets / Google Apps Script

I have the below spreadsheet that I would like to AutoFill the persons name. the issue is that there are blank rows between the names. Each name is in line with a sku2 and needs to be inline with all locations. there can be up to 10 blank rows (due to how many locations).

if I could loop this maybe

function LoopTillLr() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A2').activate();
  spreadsheet.getActiveRange().autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
  spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
};

Appreciate any help

在此处输入图像描述

If you only want to replicate the NAME values against variable LOCATION values then use this script:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSheet();
  var lastRow = ss.getDataRange().getLastRow();

  for (var i = 1; i < lastRow+1; i++) {
    if (ss.getRange(i,1).getValue() == "") {
      var value = ss.getRange(i-1,1).getValue();
      ss.getRange(i,1).setValue(value);
    }
  }
}

Ensure that A2 is not empty else the script will fail.

If it is a lot of records, you can create a function and run it. The following does this until the end of the sheet, so make sure to delete all the rows towards the end which you do not need or adjust the range in the 2nd row.

function autoFillDown(){
  const range = SpreadsheetApp.getActiveSheet().getRange("A:A");
  const rows = range.getValues();
  let outputArray = [];

  rows.forEach( row => {
    // if it contains a name, leave it
    if( row[0].length > 1){ 
      outputArray.push( [row[0]] )       
    // otherwise replace it with the value above it
    } else {
      outputArray.push( [outputArray[outputArray.length-1]] );
    }    
  });  
  range.setValues( outputArray );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM