Google 脚本:填充单元格时,提示询问用户是否要 运行 脚本

Google Script: When cell is filled, have prompt ask if user wants to run script

我写了一个脚本来清理和移动 3 个不同工作表之间的数据。用户首先将数据摘录粘贴到 "Extract" 页面,然后 运行 粘贴脚本。

当用户将数据粘贴到 "Extract" 页面的 A1 单元格时,我想要一个提示框询问用户是否要 运行 脚本;如果是,运行 脚本,如果否,则不 运行 脚本并显示消息。我该怎么做?

这就是我目前所拥有的...第一个函数工作后的一切。

function onEdit(e) { 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName("Extract");
  if(ss.s().getRange("A1").getValue() != ""){     
  var ui = SpreadsheetApp.getUi() 
  var response =   ui.alert('Would you jbot to find new claims for you?', ui.ButtonSet.OK_CANCEL);
    if (response == ui.Button.OK) {
        jbot();
    } else {
        Logger.log('The user clicked "No" or the close button in the dialog\'s title  bar.');
    }
}
}

function jbot(){
  movetobot();
  deleteRows();
  removeDupesInOtherSheets();
  deleteCol();
  cleanup();
  movetoqueue();
  message();
};

function movetobot() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName("Extract");
  var ts = ss.getSheetByName("bot");
  s.getRange("A1:BW").moveTo(ts.getRange("A1"));

}

function deleteRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName("bot");
  var r = s.getRange("BO:BO");
  var v = r.getValues();
  for(var i=v.length-1;i>=0;i--)
    if(v[0,i]=='@POSTLESSEE' || v[0,i]=='@TDI CRC_OANKURA' || v[0,i]=='@Partpaymentoffer' || v[0,i]=='@TDI_CRC_DVANKURA' || v[0,i]=='@partpaymentdupe' )
      s.deleteRow(i+1);
}

function removeDupesInOtherSheets() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s1 = ss.getSheetByName("bot").getDataRange().getValues();  
  var s2 = ss.getSheetByName("Queue").getDataRange().getValues(); 
  // iterate 'Queue' and check in 'bot' if duplicate values exist
  var nS1 = [];
  var s2Col1 = [];// data in column1 of Queue
  for(var n in s2){
    s2Col1.push(s2[n][0]);

  }
  for(var n in s1){ // iterate '180418970' and test col 1 vs col 1 in 'Queue'
    var noDup1 = checkForDup(s1[n],s2Col1)
    if(noDup1){nS1.push(noDup1)};// if not present in 'Queue' then keep

  }
  Logger.log(nS1);// view result
  ss.getSheetByName("bot").getDataRange().clear();// clear and update sheets
  ss.getSheetByName("bot").getRange(1,1,nS1.length,nS1[0].length).setValues(nS1);

}

function checkForDup(item,s){
  Logger.log(s+' = '+item[0]+'  ?')
    if(s.indexOf(item[0])>-1){
      return null;
    }
  return item;

}

function deleteCol() {
  var spread = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spread.getSheetByName("bot");
  var lastCol = sheet.getLastColumn();
  var keep = [1,2,3,4,45,53,74, 75]; // array of column numbers to keep

  for (var col=lastCol; col > 0; col--) {
    if (keep.indexOf(col) == -1) {
    // This isn't a keeper, delete it
      sheet.deleteColumn(col);

}
  }
}

function cleanup() {
  var spread = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spread.getSheetByName("bot");
  sheet.getRange("B1:B").moveTo(sheet.getRange("H1"));
  sheet.deleteColumn(2)
  sheet.deleteRow(1)
  var cell = sheet.getRange("D:D");
  cell.setNumberFormat("m/d/yy");
  var cell = sheet.getRange("F:F");
  cell.setNumberFormat("m/d/yy");
  var cell = sheet.getRange("A:A");
  cell.setHorizontalAlignment("center");
  var cell = sheet.getRange("C:D");
  cell.setHorizontalAlignment("center");
  var cell = sheet.getRange("F:G");
  cell.setHorizontalAlignment("center");
  sheet.autoResizeColumn(2)
  sheet.autoResizeColumn(5)
  sheet.autoResizeColumn(7)
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spread.getSheetByName("bot");
  var cell = sheet.getRange("BW1");
  cell.setFormula("=COUNT(A:A)");
  var spread = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spread.getSheetByName("bot");
  var range = sheet.getRange("A1:G");
  range.sort({column: 4, ascending: true})
}


function movetoqueue() { 
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
// skip row 1 (header) and beyond column G
var range = sheet.getRange(1,1,sheet.getLastRow()-1,7);
sheet = spread.getSheetByName("Queue");
var rows = sheet.getRange(1,1,sheet.getLastRow(),1).getValues();
// search for first blank row column 1
for( var i=0; i<rows.length; i++ ) {
  if( rows[i][0] === "" ) {
    var offset = sheet.getRange(1,1).offset(i,0);
    range.copyTo(offset);
    break;
  }
}  
}

function message() {
  SpreadsheetApp.getActive().getSheetByName("Queue").activate(); 
  var spread = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spread.getSheetByName("bot");
  var range = sheet.getRange("bot!BW1:BW1");
  var data = range.getValue();
  Browser.msgBox(data + " new claims have been added to the queue. Thank you for using jBot!");
}  

无论编辑哪个单元格,您的 onEdit(e) 都会触发警报。您需要检查事件范围以将其限制在 Extract 的单元格 A1 中。

function onEdit(e) {
  if( e.range.getSheet().getName() === "Extract" ) {
    if( e.range.getA1Notation() === "A1" ) {
      if( e.value !== "" ) {
        var ui = SpreadsheetApp.getUi();
        // Do the rest of your stuff here
      }
    }
  }
}