Google Sheets copy and paste row into another shee

2019-06-07 07:40发布

I have 2 sheets with an identical number of columns and all the columns have identical headers. When a project is "ready" I want to be able to select "Ready" in a data validation in that row and have that copy that row over to the Projects sheet and remove that row from the Forecasted Projects sheet. I would prefer if the contents only could be wiped from that copied row on the forecast sheet and then all other rows below it that have content could be moved up.

As is, the script will copy the "Ready" job from the forecasted projects sheet and paste it in the projects sheet in the last non empty row. However, it does not seem to want to clear the contents from the forecasted sheet. And I would like the added functionality of shifting up all the projects as soon as it clears the Ready one.

function onEdit(e) {     //Move forecasted project to Projects
  if (e.value === 'Ready') {
    var sheet = e.range.getSheet();
    if (sheet.getSheetName() == 'Projects Forecast') {    // Name of the sheet with the forecasted projects
      logSheet = e.source.getSheetByName('Projects');   // Name of the Projects sheet
      var row = e.range.getRow(); 
      var lastRow = logSheet.getLastRow();
      var range = sheet.getRange(row, 1, 1, sheet.getLastColumn()); 
      range.copyTo(logSheet.getRange(lastRow + 1, 1),{contentsOnly:true});
      sheet.clearContents(row);
    }
  }
}

1条回答
地球回转人心会变
2楼-- · 2019-06-07 08:11

To clear the contents replace sheet.clearContents(row) with

range.clearContent();

However may I suggest that you simply delete the row by replacing sheet.clearContents(row) with

sheet.deleteRow(row);

and then if you really want the same number of rows you can insert a row

sheet.insertRowsAfter(sheet.getMaxRows(), 1);

which should retain the formatting of the very last row of the sheet.

查看更多
登录 后发表回答