在记录列中的所有数据后添加弹出确认消息

时间:2017-06-27 11:40:34

标签: javascript google-apps-script google-sheets popup

我是google脚本的新手,我正在尽力修改我在网上找到的“一次编写”脚本。目前,我正在制作一个电子表格,记录实验室设备读数,如温度,湿度等。对于此电子表格,第一行是月份的日期,因此B1是第1天,AF1是第31天。第一列包含需要记录的列表,即。像温度,湿度;所以A2:A12。所有读数都将记录在B2:AF12中。第14行是记录读数的技术人员将他/她的首字母放在哪里。例如,在该月的第1天,技术人员将首字母放在B14上。

以下是我的一些修改脚本。此脚本的问题在于,每次输入读数时,都会出现一条弹出消息,要求确认该值。而不是那样,我希望在收集当天阅读的技术人员放下他/她的首字母后,该消息会出现在最后。我想让弹出窗口说出“请确认所有读数!这些读数一旦确认就无法更改”。如果选择是,那么当天的所有读数以及初始值将存储在辅助工作表和活动工作表上。如果选择否,则不会在辅助工作表上存储任何内容。技术人员将返回并纠正错误(错误与其中一个读数)。在确保一切正确后,技术人员将再次初始化,并为确认弹出窗口选择“是”。

   function onEdit() {
var firstDataRow = 2; // only take into account edits on or below this row
var lastDataRow = 14; // only take into account edits on or above this row
var firstDataColumn = 2; // only take into account edits on or to the right of this column
var lastDataColumn = 32; // only take into account edits on or to the left of this column
// naming pattern for sheets where values are copied for later checking
var helperSheetNameSuffix = "_helper";
// sheets that are free to edit with no protection
var freeToEditSheetNames = ["Free to edit 1", "Free to edit 2",   helperSheetNameSuffix + "$"];
// You can use regular expressions in sheet names. The match is not case-sensitive,
// so "free.*edit" will match "free to edit", "Free Editing for Everyone", 
// "Sheet (free to edit)" and "Free edit playground".
// Leave the last entry, helperSheetNameSuffix + "$", as it is to ensure that changes to a
// helper sheet do not trigger the creation of another _helper_helper sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = ss.getActiveSheet();
var masterSheetName = masterSheet.getName();
var masterCell = masterSheet.getActiveCell();
var sheetNameRegExp;
// do not record edits on free to edit sheets
for (var sheet in freeToEditSheetNames) {
sheetNameRegExp = new RegExp(freeToEditSheetNames[sheet], "i");
if (sheetNameRegExp.test(masterSheetName)) return;
} 
// find helper sheet
var helperSheetName = masterSheetName + helperSheetNameSuffix;
var helperSheet = ss.getSheetByName(helperSheetName);
if (helperSheet == null) { // helper sheet does not exist yet, create it as the last sheet in the spreadsheet
helperSheet = ss.insertSheet(helperSheetName, ss.getNumSheets());
Utilities.sleep(2000); // give time for the new sheet to render before going back
ss.setActiveSheet(masterSheet);
helperSheet.hideSheet();
ss.setActiveRange(masterCell);
}
if (masterCell.getRow() < firstDataRow || masterCell.getColumn() <    firstDataColumn || masterCell.getRow() > lastDataRow || masterCell.getColumn() > lastDataColumn) return;
var helperCell = helperSheet.getRange(masterCell.getA1Notation());
var newValue = masterCell.getValue(); 
var oldValue = helperCell.getValue();

var ui = SpreadsheetApp.getUi();
var response = ui.alert('Is This Entry Correct?', 'This cell cannot be changed after entering a value', ui.ButtonSet.YES_NO);
if (response == ui.Button.YES && oldValue == "") {
masterCell.setValue(newValue);  
helperCell.setValue(newValue);
} 
if (response == ui.Button.YES && oldValue !== "") {
masterCell.setValue(oldValue); Browser.msgBox('You can not change this value. Please make corrections under Comment & Date/Initial');
}
if (response ==ui.Button.NO && oldValue == "") {
Browser.msgBox('Please Enter A New Value!'); masterCell.setValue("");  return
}  
if (response ==ui.Button.NO && oldValue !== "") {
masterCell.setValue(oldValue);
}
}

谢谢

2 个答案:

答案 0 :(得分:0)

该脚本使用保留字onEdit,用于编辑简单触发器。

如果您确实/不希望每次编辑发生时都会发生某些事情,那么您应该添加JavaScript statement,如if..else,switch或类似的东西。

答案 1 :(得分:0)

使用OnEdit()功能启动功能时,每次输入或更改值时,电子表格都会连续运行脚本。

因此,当用户在新函数中插入所有值时,更简洁的方法是使用弹出消息框:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Update')
      .addItem('Update All Data', 'submitvalues')

      .addToUi();
}

但是,现在更容易创建一个按钮来指定脚本的这一位,或者通过执行此操作在帮助按钮旁边创建一个新的菜单项:

var val = mastersheet.getRange(2,sheet.getLastColumn(),12,1).getValues(); // this will take the data in the last column where you have just added the new data for 12 rows

for (var i=0;i<val.length;i++){ //this is where you start the loop
if (val[i]==""){
Browser.msgBox('Not all values are inserted, please return and insert all values')
return}// this is where you stop the function when the if statement is false
}
helpersheet.getRange(start row, start column, val.length,val[0].length).setValues(val); //this is where you set your values from the mastersheet
}

简化脚本的另一个技巧是在mastersheet中循环提交的值,并将它们作为数组写入helpersheet中。然后,您可以在该数组中检查是否有任何单元格为空并返回错误框。然后,您不必创建当前拥有的所有变量。

修改 只是一个示例,您必须适应您的脚本,当您完成第一个提交框消息时,将其插入到最后的submitvalues函数中:

prepare(for:sender:)