我有一个Google表单,可以将数据插入Google表格,然后该表格中的脚本会为该行生成一个数字。除了生成的数字之外,所有信息都输入到表格中。我找到了一个脚本,根据该信息创建PDF,添加QR码并通过电子邮件发送。该脚本使用Google Doc作为PDF的模板。所有这些都很有效但我需要找到一种方法来获取工作表中生成的数字,以便我可以使用它来创建QR代码。
我想也许我可以使用脚本预先填写表单上的最后一个数字的方框,但Google表格似乎不可能。然后我找到了一些代码可以获取特定列的最后一行,但是我无法使用我正在使用的现有代码。有谁知道我正在寻找的方法吗?
表格看起来与此类似:
|--------------|---------------|------------------|-----------------|
| prefix | number | first_name | last_name |
|--------------|---------------|------------------|-----------------|
| AA | 111 | FExample1 | LExample1 |
|--------------|---------------|------------------|-----------------|
| AB | 112 | FExample2 | LExample2 |
|--------------|---------------|------------------|-----------------|
| AA | 113 | FExample3 | LExample3 |
|--------------|---------------|------------------|-----------------|
我正在使用的代码:
// TJ Houston tjhouston.com (tj@tjhouston.com)
// Get template from Google Docs and name it
var docTemplate = "documentid"; // *** replace with your template ID ***
var docName = "label";
// When Form Gets submitted
function onFormSubmit(e) {
//Create number
var sheet = SpreadsheetApp.getActiveSheet();
var row = SpreadsheetApp.getActiveSheet().getLastRow();
sheet.getRange(row,2).setValue(row+5871);
//Get information from form and set as variables
var email_address = "email@example.com";
var last_name = e.values[5];
var first_name = e.values[4];
// Get document template, copy it as a new temp doc, and save the Doc’s id
//var copyId = DocsList.getFileById(docTemplate)
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+last_name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('key_LN', last_name);
copyBody.replaceText('key_FN', first_name);
// Insert barcode
var image = UrlFetchApp.fetch('http://chart.apis.google.com/chart?cht=qr&chs=150x150&chl=" + prefix + "" + number + "&chld=L%7C1')
var totalElements = copyBody.getNumChildren();
var el=[]
for( var j = 0; j < totalElements; ++j ) {
var element = copyBody.getChild(j);
var type = element.getType();
Logger.log(j+" : "+type);// to see doc's content
if (type =='PARAGRAPH'){
el[j]=element.getText()
if(el[j]=='###'){element.removeFromParent();// remove the ###
copyBody.insertImage(j, image);// 'image' is the image file as blob
}
}
}
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Label for " + last_name + "";
var body = "Attached is the label for " + last_name + ";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
答案 0 :(得分:0)
从响应表Google表格最后一行的特定列获取数据。 试试这个
function getlastdata(x){
var s_ID = "1yLLsfU4tgR6_0Xny7CMjqKKALoHTrYLJybPyWHUwkQI"; //spreadsheet id
var sh = SpreadsheetApp.openById(s_ID),
s = sh.setActiveSheet(sh.getSheets()[0]),
lr = s.getLastRow(),
lc = s.getLastColumn();
var obj = s.getRange(lr,1,1,lc).activate().getValues();
Logger.log(obj[0][x])
}
与: s_ID 必须是您的响应电子表格ID 和 x ,作为特定列的 num