简体   繁体   English

如果单元格包含文本,则添加新行

[英]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.我正在尝试找到一种在填充空单元格时添加新行的方法。目前第 15 行有一个空行。如果将文本添加到单元格 A15,我希望添加另一个空行所以深灰色分隔符上方总是有一个空白行。 The same would go for A22 and other cells on the sheet. A22 和工作表上的其他单元格也是如此。

工作表截图

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:您可以尝试下面使用onEdit()触发器的示例脚本,每次输入 A 列上的空单元格时,空白行将被复制并放置在灰色分隔符上方:

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 :在单元格 A15 和 A22 上键入任何文本然后按 Enter 后,空白行将被复制并放置在灰色分隔符上方

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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