我正在制作一个谷歌表单,我有一个名为 name 的字段,其中包含标题、公司和电子邮件地址等其他字段。如果数据库中已经有一个特定的人,我希望其他信息用新信息替换旧信息(即更新功能),但是我在使用 Google Apps 脚本时遇到了麻烦,因为我找到了文档比较可怜。有人介意帮我一把吗?
问问题
18080 次
2 回答
4
这不会阻止谷歌表单首先提交重复值,但我认为你想要的看起来像......
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 行示例,尽管如果您浏览所有教程,则会有更多完成的示例。开发人员更多地需要制作这些类型的脚本。
于 2013-06-06T19:44:42.713 回答
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
于 2020-04-16T16:56:04.387 回答