getUi()警告不执行响应

时间:2017-11-21 16:17:47

标签: google-apps-script google-sheets

此脚本会将Google表格中的一行从一张到另一张。没有警报框,它工作正常。我想在移动前添加一个框进行确认。框显示,但单击“是”时没有任何反应。我也尝试了一个提示,但没有结果。

如何正确使用此功能?

function onEdit(event) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  var sourceSheet = "Source"; // Name of sheet copy from
  var targetSheet = "Destination"; // Name of sheet copy to
  var targetSheet = ss.getSheetByName(targetSheet);
  var ui = SpreadsheetApp.getUi();

function CountColB(){  // Function to get first filled column B, starting from bottom.
    var data = targetSheet.getDataRange().getValues();
  for(var i = data.length-1 ; i >=0 ; i--){
    if (data[i][1] != null && data[i][1] != ''){
      return i+1 ;
    }}}
var lastRow = CountColB();

  if(s.getName() == sourceSheet && r.getColumn() == 1 && r.getValue() == "X") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var target = targetSheet.getRange(lastRow + 1, 2);
    ui.alert('Move Row?', ui.ButtonSet.YES_NO);
    if (response == ui.Button.YES) {
    s.getRange(row, 2, 1, numColumns).moveTo(target);
    s.deleteRow(row);
 }

  }
}

1 个答案:

答案 0 :(得分:1)

您没有设置回复'变量是快速响应。试试这个if语句:

  if(s.getName() == sourceSheet && r.getColumn() == 1 && r.getValue() == "X") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var target = targetSheet.getRange(lastRow + 1, 2);
    var response = ui.alert('Move Row?', ui.ButtonSet.YES_NO);
    if (response == ui.Button.YES) {
    s.getRange(row, 2, 1, numColumns).moveTo(target);
    s.deleteRow(row);
 }