我正在制作一个谷歌表单,我有一个名为name的字段,其他字段包括标题,公司和电子邮件地址。如果数据库中已经有特定的人,我希望其他信息用新信息(即更新功能)替换旧信息,但是由于我找到了文档,我在使用Google Apps脚本时遇到了麻烦相当可悲。有人会介意帮我一把吗?
答案 0 :(得分:2)
这不会阻止Google表单首先提交重复值,但我认为你想要的东西看起来像......
function updateExisting() {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
s = ss.getSheetByName('Sheet1'),
lastRow = s.getLastRow(),
lastValues = s.getRange('A'+lastRow+':E'+lastRow).getValues(),
name = lastValues[0][0],
allNames = s.getRange('A2:A').getValues(),
row, len;
// TRY AND FIND EXISTING NAME
for (row = 0, len = allNames.length; row < len - 1; row++)
if (allNames[row][0] == name) {
// OVERWRITE OLD DATA
s.getRange('A2').offset(0, 0, row, lastValues.length).setValues([lastValues]);
// DELETE THE LAST ROW
s.deleteRow(lastRow);
break;}
}
这必须由工作表内的表单提交触发器触发。
文件可能会让人不知所措。他们通常只做1或2行示例,但是如果你遍历所有tutorials,那么会有更多完成的例子。制作这些类型的脚本的开发人员更多。答案 1 :(得分:0)
这是我解决的方法...
我用他们的电子邮件地址检查重复项,但您可以使用任何想要的内容:
function SendConfirmationMail(e) {
// Fetch data from latest submission on spreadsheet
var ss = SpreadsheetApp.getActiveSheet();
// Access the workbook
var wrkBk = SpreadsheetApp.getActiveSpreadsheet();
// Fetch the necessary sheets
var ssResponses = wrkBk.getSheetByName("Form Responses");
var ssAutomailer = wrkBk.getSheetByName("Automailer"); // <---this sheet is in another tab that has the email's subject and body so this script can be dynamically updated.
// Fetch & store data from form submissions
var numRows = ssResponses.getLastRow(); // <--- store # of total rows
var lastfNameCell = "B" + numRows; // <--- store the range of last cell containing fName data
var lastEmailCell = "C" + numRows; // <--- store the range of last cell containing email data
var numPrevRows = numRows -1; // <--- store the # of previous rows
var lastPrevEmailCell = "C" + numPrevRows; // <--- store the range of last "previous" cell that contains email data in A1 notation
var lastPrevEmailRange = "C2:" + lastPrevEmailCell; // <--- store range of ALL previous cells containing email data in A1 notation
var fName = ssResponses.getRange(lastfNameCell).getValue(); // <--- store the fName from latest submission
var email = ssResponses.getRange(lastEmailCell).getValue(); // <--- store the email address from latest submission
var prevEmails = ssResponses.getRange(lastPrevEmailRange).getValues(); // <--- store range of all previous email addresses into an array
// Convert email list to string for search functionality
prevEmails = prevEmails.toString();
// Run an index search to see if the email address already exists
// If no match is found, -1 will be the result and we can continue on...
if (prevEmails.indexOf(email) == "-1") {
// Fetch own email address for cc functionality
var cc = Session.getActiveUser().getEmail();
// Set sender's name
var sendername = "Your Site/Business Name Goes Here"
// Store data from Automailer cells
var subject = ssAutomailer.getRange('A3').getValue();
var body = ssAutomailer.getRange('B3').getValue();
// Store HTML template and it's contents
var htmlFile = HtmlService.createTemplateFromFile("new-subscriber-template.html");
var htmlContent = htmlFile.evaluate().getContent();
// Convert spreadsheet body content to HTML
var htmlBody = body.replace(/\n/g, '<br>'); //<----- converts newlines to HTML format
// Replace placeholder data in htmlContent
htmlContent = htmlContent.replace("[fName]", fName);
htmlContent = htmlContent.replace("[body]", htmlBody);
// Add a personalized greeting to plain text body and store to new variable
var textbody = "Hi " + fName + ",\n\n" + body;
// Send the email!
GmailApp.sendEmail(email, subject, textbody,
// Extra email paramaters: un-comment the first section of this line to send yourself a carbon copy.
{/*cc: cc, */name: sendername, htmlBody: htmlContent});
}
// If the index search found a duplicate, do this instead
else {
// Send error notification email
GmailApp.sendEmail(email, "There was an error with your request...", "Looks like you're already a subscriber!",
// Extra email paramaters: un-comment the first section of this line to send yourself a carbon copy.
{/*cc: cc, */name: sendername, htmlBody: "Looks like you're already a subscriber!" });
// Be gone duplicate!
ssResponses.deleteRow(numRows);
}
}
请记住,如果要搜索除电子邮件以外的几乎所有内容,则可能需要使用一些额外的搜索参数来排除误报。我打算使用以下内容,但我认为电子邮件的独特性足以避免代码中包含此多余的行:
var emailSearch = ',' + email + ',' <--- commas on either end added to match the entire cell on the index search