如何使用Google脚本通过电子邮件从电子表格发送数据

时间:2018-10-15 11:39:24

标签: email google-apps-script

我正在使用Google表单进行在线注册。一个人提交后,就会向他们输入的电子邮件发送一封电子邮件。数据就是他们在表单中输入的东西。因此,此电子邮件用作数据确认电子邮件。现在的问题是,我所做的测试成功了,但是电子邮件中包含了所有数据。我的问题是,如何通过电子邮件发送来自特定行的数据

这是我所做的(由某人复制)

      function sendEmail() {

 //setup function
 var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var StartRow = 3;
 var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
 var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
 var AllValues = WholeRange.getValues();

 var message = "";
  //iterate loop
 for (i in AllValues) {

 //set current row
 var CurrentRow = AllValues[i];

 //define column to check if sent
 var EmailSent = CurrentRow[11];

 //if row has been sent, then continue to next iteration
 if (EmailSent == "sent") 
 continue;

 //set HTML template for information
 message +=
  "<p><b>Found by: </b>" + CurrentRow[1] + "</p>" +
  "<p><b>Title: </b>" + CurrentRow[2] + "</p>" +
  "<p><b>Agency: </b>" + CurrentRow[3] + "</p>" +
  "<p><b>Summary: </b>" + CurrentRow[4] + "</p>" +
  "<p><b>Due: </b>" + CurrentRow[5] + "</p>" +
  "<p><b>Posted: </b>" + CurrentRow[6] + "</p>" +
  "<p><b>Total Funding: </b>" + CurrentRow[7] + "</p>" +
  "<p><b>Announcement Number: </b>" + CurrentRow[8] + "</p>" +
  "<p><b>Useful Links: </b>" + CurrentRow[9] + "</p><br><br>";

  //set the row to look at
  var setRow = parseInt(i) + StartRow;

  //mark row as "sent"
  ActiveSheet.getRange(setRow, 11).setValue("sent");
  }

 //define who to send grants to 
 var SendTo = "emailaddress1@gmail.com" + "," + "emailaddress2@gmail.com";

 //set subject line
 var Subject = "Grants";


  //send the actual email  
  MailApp.sendEmail({
  to: SendTo,
  cc: "",
  subject: Subject,
  htmlBody: message,
   });
    }

2 个答案:

答案 0 :(得分:0)

我最近对此回答了similar question。对于我来说,将问题分解成我们所需的部分总是比较容易的。在这种情况下,我们需要:所有响应,然后我们需要匹配一个响应

最好的方法是先将表单响应读取为数​​组:

from keras.preprocessing.text import Tokenizer
from keras.layers import Dense
from keras.models import Sequential

data = open('movie_lines.txt', encoding='ISO-8859-1')
lines = [line for line in data]
filtered_lines = []

for line in lines:
    sentence = line.split('+++$+++')[4].strip(' ')
    filtered_lines.append(sentence)

train_size = int(len(filtered_lines) * .8)
train_portion = filtered_lines[:train_size]
test_portion = filtered_lines[train_size:]

x_lines = train_portion[::2]
y_lines = train_portion[1::2]
x_test = test_portion[::2]
y_test = test_portion[1::2]
vocab_size = 1000
print('Prepared data')

def prepare_text(text):
    tokenizer = Tokenizer(num_words=vocab_size)
    tokenizer.fit_on_texts(text)
    matrix = tokenizer.texts_to_matrix(text)
    return matrix


x_train = prepare_text(x_lines)
print('matrixed x')
y_train = prepare_text(y_lines)
print('matrixed y')
print(f'X shape: {x_train.shape}')
print(f'Y shape: {y_train.shape}')

model = Sequential()
model.add(Dense(512, input_shape=(vocab_size,), activation='relu'))
model.add(Dense(len(y_lines), activation='softmax'))

model.compile(
        loss='categorical_crossentropy',
        optimizer='adam',
        metrics=['accuracy',]
        )
print('Created and compiled model')

model.fit(x_train, y_train, epochs=3)

score = model.evaluate(x_test, y_test, batch_size=32, epochs=3)
print('Test Score:'+score[0])
print('Test Accuracy:'+score[1])

如果您知道此人的电子邮件,则假设他们是第一位输入电子邮件的人,我们可以得到这样的答复:

function formResponsesToArray() {
  var app = FormApp.openById(...),
  responses = app.getResponses(), stringResponses = []

  responses.forEach(function(r) {
    var response = []
    r.getItemResponses().forEach(function(i) {
      response.push(i.getResponse())
    })

    stringResponses.push(response)
  })

  Logger.log(stringResponses)
}

然后,您可以使用他们的响应来获取他们输入到表单中的信息!

答案 1 :(得分:0)

您共享的代码基本上会收集电子邮件状态字段没有“已发送”值的所有行数据,并在末尾撰写一条消息并将其发送。 根据您的情况,您需要为电子邮件状态字段没有“已发送”值的每一行发送不同的邮件。

我在循环内添加了mailApp语法,仅用于发送不同的邮件,并对消息变量进行了一些更改。

 function sendEmail() {

 //setup function
 var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var StartRow = 3;
 var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
 var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
 var AllValues = WholeRange.getValues();

  //iterate loop
 for (i in AllValues) {

 //set current row
 var CurrentRow = AllValues[i];

 //define column to check if sent
 var EmailSent = CurrentRow[11];

 //if row has been sent, then continue to next iteration
 if (EmailSent == "sent") 
 continue;

 //set HTML template for information
 //here i have removed the "+" sign to create a new message for its respective row
var message=
  "<p><b>Found by: </b>" + CurrentRow[1] + "</p>" +
  "<p><b>Title: </b>" + CurrentRow[2] + "</p>" +
  "<p><b>Agency: </b>" + CurrentRow[3] + "</p>" +
  "<p><b>Summary: </b>" + CurrentRow[4] + "</p>" +
  "<p><b>Due: </b>" + CurrentRow[5] + "</p>" +
  "<p><b>Posted: </b>" + CurrentRow[6] + "</p>" +
  "<p><b>Total Funding: </b>" + CurrentRow[7] + "</p>" +
  "<p><b>Announcement Number: </b>" + CurrentRow[8] + "</p>" +
  "<p><b>Useful Links: </b>" + CurrentRow[9] + "</p><br><br>";

  //here you can add the email address field to whom you want to send emails.
  //e.g. var SendTo=CurrentRow[1];
   //define who to send grants to 
   var SendTo = "emailaddress1@gmail.com" + "," + "emailaddress2@gmail.com";

   //set subject line
   var Subject = "Grants";

    //send the actual email  
    MailApp.sendEmail({
    to: SendTo,
    cc: "",
    subject: Subject,
    htmlBody: message,
     });


  //set the row to look at
  var setRow = parseInt(i) + StartRow;

  //mark row as "sent"
  ActiveSheet.getRange(setRow, 11).setValue("sent");
  }
  }

希望这会有所帮助,谢谢