Google电子表格 - 如何在执行脚本之前添加确认消息框

时间:2014-03-12 06:23:29

标签: google-apps-script google-sheets messagebox

我想在脚本运行之前添加一个确认消息框。

这是脚本:

function moveTo(e) {
// moves a row from a sheet to another when a magic value is entered in a column
// adjust the following variables to fit your needs try {

var sheetNameToWatch = "Active Discs";
var columnNumberToWatch = 15; // column A = 1, B = 2, etc.
var valueToWatch = "Completed";
var sheetNameToMoveTheRowTo = "CompletedShipped Projects";


var ss = e.source;
var sheet = ss.getActiveSheet();
var range = e.range;

if (sheet.getName() == sheetNameToWatch && range.columnStart == columnNumberToWatch && e.value == valueToWatch) {

  var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
  var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
  sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
  sheet.deleteRow(range.getRow());
}
} catch (error) { Logger.log(error)
}

消息框将包含以下消息设置: 该消息应为"您确认将Ocean Gil移至已完成的发货项目表吗?"

var app = UiApp.createApplication().setHeight(150).setWidth(250);
var msg = "Do you confirm to move " + "Active row cells A + B" + " to Completed Shipped   Projects Sheet?";
app.setTitle("Please Confirm this Action");
app.add(app.createVerticalPanel().add(app.createLabel(msg)));
var doc = SpreadsheetApp.getActive();
doc.show(app);
}

如果该人选择是,那么" moveTo"脚本执行。如果不是,则脚本不执行任何操作并关闭提示窗口。

以下是共享的Google电子表格。

https://docs.google.com/spreadsheet/ccc?key=0Ag8NytPhOo00dER6WG1PQ3g3V1hCbnVUaTJUZDFZenc&usp=sharing

我们非常感谢您对代码的任何帮助。在此先感谢。

1 个答案:

答案 0 :(得分:1)

在这种情况下使用Browser类可能会更简单,更紧凑,请参阅doc here with example code

编辑:在您的代码中,您可以像这样使用它:

function moveTo(e) {
// moves a row from a sheet to another when a magic value is entered in a column
// adjust the following variables to fit your needs try {

var sheetNameToWatch = "Active Discs";
var columnNumberToWatch = 15; // column A = 1, B = 2, etc.
var valueToWatch = "Completed";
var sheetNameToMoveTheRowTo = "CompletedShipped Projects";

  try{
var ss = e.source;
var sheet = ss.getActiveSheet();
var range = e.range;

if (sheet.getName() == sheetNameToWatch && range.columnStart == columnNumberToWatch && e.value == valueToWatch) {
  var confirm = Browser.msgBox('Do you want to move data to the other sheet ?', Browser.Buttons.YES_NO); 
  if(confirm!='yes'){return};// if user click NO then exit the function, else move data
  var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
  var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
  sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
  sheet.deleteRow(range.getRow());
}
  } catch (error) { Logger.log(error)}
}