简体   繁体   中英

Add new row if cell contains text

I'm trying to find a way to add a new row when an empty cell is filled in. At the moment there is an empty row on row 15. If text is added to cell A15 I'd like another empty row to be added so there is always a blank row above the dark gray separator. The same would go for A22 and other cells on the sheet.

工作表截图

Suggestion

You can try this sample script below that uses onEdit() trigger, where every time you input to an empty cell on column A, the blank row will be copied and placed above the gray separator:

function onEdit() {
  var ss = SpreadsheetApp.getActive().getActiveSheet();
  var refRow = ss.getActiveCell().getRow();
  var newRow = refRow + 1;
  var refCol = ss.getDataRange().getLastColumn();
  var RefRow = ss.getRange(refRow,1,refRow,refCol);

  if(ss.getActiveRange().getColumn() !=1) return; //Ensures to only add new blank row above the gray separator when a cell on column A is selected/edited

  RefRow.copyTo(ss.getRange(newRow, 1));
  ss.getRange("A"+newRow).clear();
}

Reference code used from this post

Sample Test

Sample test sheet with design based on the screenshot you've shared:

在此处输入图片说明

After typing any text on the Cell A15 & A22 and then pressing Enter, the blank row gets copied & placed above the gray separator :

在此处输入图片说明

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-2024 STACKOOM.COM