Google Apps 脚本将数据从表单条目移动到日志 w/ UI 输入驱动的 If 语句
Google Apps Script to Move Data from Form Entry to Log w/ UI Input Driven If Statements
我对 Google 的 Apps 脚本的经验有限,但到目前为止,我已经设法将功能代码拼凑在一起。最终,我要做的是创建几个 Apps 脚本连接按钮:“新建”、“保存”、“下载”和“关闭”。到目前为止,对我来说最困难的是“关闭”按钮。当用户点击“关闭”时,系统会首先提示用户确认是否要“关闭”数据表单。单击“是”后,Apps 脚本应检查 CAR 日志中是否存在匹配的 CAR 编号(CAR 编号是唯一的),如果找到,则通过对话框提示用户确认匹配的“CAR 日志”中的数据行应该被“报名表”中的数据覆盖。如果用户点击“YES”,“Entry Form”中的数据将被复制并粘贴到“Car Log”中具有匹配 CAR No 的行。如果用户点击“NO”,包含“Entry Form”数据的单元格" 将被清除,以便可以输入新数据。
示例 Google Sheet 带报名表和 CAR 日志:
https://docs.google.com/spreadsheets/d/1XyOldXz7FoZeAAeToWFbNXJlbHWlgwRkVlo9ifnUZy8/edit?usp=sharing
预期流程是:
这是我到目前为止编写的代码:
function FormClose() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var uiConfirmClose = SpreadsheetApp.getUi();
var responseConfirmClose = uiConfirmClose.alert('Close & Clear all Form Data', 'Are you sure?', uiConfirmClose.ButtonSet.YES_NO);
var destination = ss.getSheetByName('CAR Log');
var CurrentCARNo = ss.getRange("D5").getValue();
var UsedCARNosRange = SpreadsheetApp.openById("MYSPREADSHEETID").getSheetByName("CAR Log").getRange("A:A");
var UsedCARNos = UsedCARNosRange.getValues();
var uiOverwrite = SpreadsheetApp.getUi();
var responseOverwrite = uiOverwrite.alert('CAR No. '+CurrentCARNo+' Already Exists in Log', 'Would you like to overwrite the log with the data in this form?', uiOverwrite.ButtonSet.YES_NO);
// Confirm Close: YES
if (responseConfirmClose == uiConfirmClose.Button.YES) {
Logger.log('The user clicked "Yes."');
// Check for Existing CAR No.
for (var i in UsedCARNos){
// FOUND
if (UsedCARNos[i][0].match(CurrentCARNo)!=null){
// Overwrite?
if (responseOverwrite == uiOverwrite.Button.YES) {
Logger.log('The user clicked "Yes."');
ss.getRange('D5:E5').copyTo(destination.getRange(destination.getLastRow()+1,1,1,1),SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
}
else {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
}
// NOT FOUND
else {
Logger.log('Data not found. Copy data to log.');
ss.getRange('D5:E5').copyTo(destination.getRange(destination.getLastRow()+1,1,1,1),SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
}
}
}
// Confirm Close: NO
if (responseConfirmClose == uiConfirmClose.Button.NO) {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
return;
};
}
如有任何指导,我将不胜感激。
如果我清楚地理解您的问题,您的主要目标是在您的第一个对话框提示上单击 否 按钮时从 运行ning 停止您的脚本(responseConfirmClose
) :
调查结果
- 在您的脚本中,
responseOverwrite
运行 紧接着第一个
对话框 responseConfirmClose
提示,即使点击 否,因为它正在 初始化 & 运行 在代码的开头。
建议
- 按照你的逻辑,我建议把你的
responseOverwrite
第一个 if
条件语句中的变量(如果
YES 按钮在 responseConfirmClose
对话框中被选中)如下面经过调整的脚本所示:
[更新]
调整脚本
function runClose() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var uiConfirmClose = SpreadsheetApp.getUi();
var destination = ss.getSheetByName('CAR Log');
var carLogData = destination.getDataRange().getDisplayValues();
var uiOverwrite = SpreadsheetApp.getUi();
var entryFormData = ss.getSheetByName('Entry Form').getDataRange().getDisplayValues();
var responseConfirmClose = uiConfirmClose.alert('Close & Clear all Form Data', 'Are you sure?', uiConfirmClose.ButtonSet.YES_NO);
if (responseConfirmClose == uiConfirmClose.Button.YES) {
Logger.log('The user clicked "Yes."');
for(var i in carLogData){
var row = parseInt(i) +1;
if(carLogData[i][0] == entryFormData[4][3]){
Logger.log("Found a match for \""+"CAR NO. "+entryFormData[4][3]+"\" in row #"+row+ " on the \"CAR Log\" sheet");
var responseOverwrite = uiOverwrite.alert('CAR No. '+entryFormData[4][3]+' Already Exists in Log', 'Would you like to overwrite the log with the data in this form?', uiOverwrite.ButtonSet.YES_NO);
// Overwrite?
if (responseOverwrite == uiOverwrite.Button.YES) {
Logger.log('The user clicked "Yes."');
var copyData = [[entryFormData[4][3], //CAR No
entryFormData[8][3], //Date Opened
entryFormData[8][9], //Originator
entryFormData[8][15], //NCP # (if applicable)
entryFormData[10][2], //Action Type
entryFormData[10][10], //Source
entryFormData[12][1], //Issue to
entryFormData[12][10], //QMS Reference & Clause
entryFormData[17][0], //Description of Nonconformance / Opportunity for Improvement
entryFormData[19][0], //Root Cause
entryFormData[21][0], //Corrective Action
entryFormData[23][5], //Corrective Action Discussed with and Agreed Upon b
entryFormData[23][15], //Corrective Action Planned Completion Date
entryFormData[28][3], //Approved by
entryFormData[28][11], //Date Approve
entryFormData[33][3], //Followed up by
entryFormData[33][11], //Date of Follow up
entryFormData[35][5], //Corrective Action Effective?
entryFormData[38][0], //Evidence
entryFormData[40][3], //Close Out by
entryFormData[40][11]]]; //Date Closed
destination.getRange("A"+row+":U"+row).setValues(copyData);
}
else {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
}
}
}else{
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
//Code here to empty the fields on "Entry Form" e.q.:
ss.getRange("D9:E9").clearContent();
ss.getRange("J9:K9").clearContent();
ss.getRange("P9:Q9").clearContent();
ss.getRange("C11:I11").clearContent();
ss.getRange("K11:O11").clearContent();
ss.getRange("B13:F13").clearContent();
ss.getRange("K13:P14").clearContent();
//continue here for the rest of the fields
}
示例演示:
- 样本
Entry Form
数据
- 样本
CAR Log
数据
- 在对话框提示中单击 YES 后,由于
CAR Log
的第 4 行已经有匹配的 CAR No
,它将被覆盖有新数据:
Entry Form
之后会被清除或者在提示中点击否时会被清除:
我对 Google 的 Apps 脚本的经验有限,但到目前为止,我已经设法将功能代码拼凑在一起。最终,我要做的是创建几个 Apps 脚本连接按钮:“新建”、“保存”、“下载”和“关闭”。到目前为止,对我来说最困难的是“关闭”按钮。当用户点击“关闭”时,系统会首先提示用户确认是否要“关闭”数据表单。单击“是”后,Apps 脚本应检查 CAR 日志中是否存在匹配的 CAR 编号(CAR 编号是唯一的),如果找到,则通过对话框提示用户确认匹配的“CAR 日志”中的数据行应该被“报名表”中的数据覆盖。如果用户点击“YES”,“Entry Form”中的数据将被复制并粘贴到“Car Log”中具有匹配 CAR No 的行。如果用户点击“NO”,包含“Entry Form”数据的单元格" 将被清除,以便可以输入新数据。
示例 Google Sheet 带报名表和 CAR 日志: https://docs.google.com/spreadsheets/d/1XyOldXz7FoZeAAeToWFbNXJlbHWlgwRkVlo9ifnUZy8/edit?usp=sharing
预期流程是:
这是我到目前为止编写的代码:
function FormClose() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var uiConfirmClose = SpreadsheetApp.getUi();
var responseConfirmClose = uiConfirmClose.alert('Close & Clear all Form Data', 'Are you sure?', uiConfirmClose.ButtonSet.YES_NO);
var destination = ss.getSheetByName('CAR Log');
var CurrentCARNo = ss.getRange("D5").getValue();
var UsedCARNosRange = SpreadsheetApp.openById("MYSPREADSHEETID").getSheetByName("CAR Log").getRange("A:A");
var UsedCARNos = UsedCARNosRange.getValues();
var uiOverwrite = SpreadsheetApp.getUi();
var responseOverwrite = uiOverwrite.alert('CAR No. '+CurrentCARNo+' Already Exists in Log', 'Would you like to overwrite the log with the data in this form?', uiOverwrite.ButtonSet.YES_NO);
// Confirm Close: YES
if (responseConfirmClose == uiConfirmClose.Button.YES) {
Logger.log('The user clicked "Yes."');
// Check for Existing CAR No.
for (var i in UsedCARNos){
// FOUND
if (UsedCARNos[i][0].match(CurrentCARNo)!=null){
// Overwrite?
if (responseOverwrite == uiOverwrite.Button.YES) {
Logger.log('The user clicked "Yes."');
ss.getRange('D5:E5').copyTo(destination.getRange(destination.getLastRow()+1,1,1,1),SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
}
else {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
}
// NOT FOUND
else {
Logger.log('Data not found. Copy data to log.');
ss.getRange('D5:E5').copyTo(destination.getRange(destination.getLastRow()+1,1,1,1),SpreadsheetApp.CopyPasteType.PASTE_VALUES,true);
}
}
}
// Confirm Close: NO
if (responseConfirmClose == uiConfirmClose.Button.NO) {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
return;
};
}
如有任何指导,我将不胜感激。
如果我清楚地理解您的问题,您的主要目标是在您的第一个对话框提示上单击 否 按钮时从 运行ning 停止您的脚本(responseConfirmClose
) :
调查结果
- 在您的脚本中,
responseOverwrite
运行 紧接着第一个 对话框responseConfirmClose
提示,即使点击 否,因为它正在 初始化 & 运行 在代码的开头。
建议
- 按照你的逻辑,我建议把你的
responseOverwrite
第一个if
条件语句中的变量(如果 YES 按钮在responseConfirmClose
对话框中被选中)如下面经过调整的脚本所示:
[更新]
调整脚本
function runClose() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var uiConfirmClose = SpreadsheetApp.getUi();
var destination = ss.getSheetByName('CAR Log');
var carLogData = destination.getDataRange().getDisplayValues();
var uiOverwrite = SpreadsheetApp.getUi();
var entryFormData = ss.getSheetByName('Entry Form').getDataRange().getDisplayValues();
var responseConfirmClose = uiConfirmClose.alert('Close & Clear all Form Data', 'Are you sure?', uiConfirmClose.ButtonSet.YES_NO);
if (responseConfirmClose == uiConfirmClose.Button.YES) {
Logger.log('The user clicked "Yes."');
for(var i in carLogData){
var row = parseInt(i) +1;
if(carLogData[i][0] == entryFormData[4][3]){
Logger.log("Found a match for \""+"CAR NO. "+entryFormData[4][3]+"\" in row #"+row+ " on the \"CAR Log\" sheet");
var responseOverwrite = uiOverwrite.alert('CAR No. '+entryFormData[4][3]+' Already Exists in Log', 'Would you like to overwrite the log with the data in this form?', uiOverwrite.ButtonSet.YES_NO);
// Overwrite?
if (responseOverwrite == uiOverwrite.Button.YES) {
Logger.log('The user clicked "Yes."');
var copyData = [[entryFormData[4][3], //CAR No
entryFormData[8][3], //Date Opened
entryFormData[8][9], //Originator
entryFormData[8][15], //NCP # (if applicable)
entryFormData[10][2], //Action Type
entryFormData[10][10], //Source
entryFormData[12][1], //Issue to
entryFormData[12][10], //QMS Reference & Clause
entryFormData[17][0], //Description of Nonconformance / Opportunity for Improvement
entryFormData[19][0], //Root Cause
entryFormData[21][0], //Corrective Action
entryFormData[23][5], //Corrective Action Discussed with and Agreed Upon b
entryFormData[23][15], //Corrective Action Planned Completion Date
entryFormData[28][3], //Approved by
entryFormData[28][11], //Date Approve
entryFormData[33][3], //Followed up by
entryFormData[33][11], //Date of Follow up
entryFormData[35][5], //Corrective Action Effective?
entryFormData[38][0], //Evidence
entryFormData[40][3], //Close Out by
entryFormData[40][11]]]; //Date Closed
destination.getRange("A"+row+":U"+row).setValues(copyData);
}
else {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
}
}
}else{
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
//Code here to empty the fields on "Entry Form" e.q.:
ss.getRange("D9:E9").clearContent();
ss.getRange("J9:K9").clearContent();
ss.getRange("P9:Q9").clearContent();
ss.getRange("C11:I11").clearContent();
ss.getRange("K11:O11").clearContent();
ss.getRange("B13:F13").clearContent();
ss.getRange("K13:P14").clearContent();
//continue here for the rest of the fields
}
示例演示:
- 样本
Entry Form
数据
- 样本
CAR Log
数据
- 在对话框提示中单击 YES 后,由于
CAR Log
的第 4 行已经有匹配的CAR No
,它将被覆盖有新数据:
Entry Form
之后会被清除或者在提示中点击否时会被清除: