从getActiveCell()更改;改为获得范围

时间:2018-03-21 17:52:28

标签: google-apps-script

这对你们来说可能相当简单。

如何替换getActiveCell()我不需要在线运行脚本,所以我可以根据时间触发设置它?

检查下面的代码块。

function MoveRows() {
  var sheetNameToWatch = "Requests";
  var sheetNameToMoveTo = "Approved";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveCell();

  var range = sheet.getActiveCell();
  var row = range.getRow();

  if (sheet.getName() == sheetNameToWatch &&
    sheet.getRange(row, 8).getValue() == "Y" &&
    sheet.getRange(row, 12).getValue() == "Approved") {
{

    var targetSheet = ss.getSheetByName(sheetNameToMoveTo);
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    sheet.getRange(range.getRow(), 1, 1, 
sheet.getLastColumn()).moveTo(targetRange);
    sheet.deleteRow(range.getRow());
  }
    }}

谢谢!

1 个答案:

答案 0 :(得分:0)

在看了一下你的代码后,我猜这可能接近你所寻找的。

function MoveRows() {
  var sheetNameToWatch = "Requests";
  var sheetNameToMoveTo = "Approved";
  var ss = SpreadsheetApp.openById(id)
  var sheet = ss.getSheetByName('Requests')
  var range = sheet.getRange(sheet.getLastRow(),1,1,sheet.getLastColumn());
  var values=range.getValues();
  var row = range.getRow();
  if (values[0][7] == "Y" && values[0][11] == "Approved") {
    var targetSheet = ss.getSheetByName(sheetNameToMoveTo);
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    range.moveTo(targetRange);//you could also just do targetSheet.appendRow(values[0]); Your call
    sheet.deleteRow(range.getRow());
  }
}