我有一个谷歌表单,可以在电子表格中捕获回复。它每次都会在新响应时创建一个新工作表。
我现在正在尝试添加"邮件活动表脚本"到创建新工作表的现有脚本。
但是我收到错误" https://docs.google.com/spreadsheets/d/SS_ID/export请求失败?"围绕以下
var result = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
我已将此文章用作发送电子邮件部分的guide。
这是我正在使用的整个脚本。
function onSubmit(e){
Logger.log('submit ran');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get last row of data
var lastRow = sheet.getLastRow();
var colB_Data = sheet.getRange(lastRow, 2).getValue();
//create sheet name with order no.
ss.insertSheet(colB_Data);
//order formula
var sheets = ss.getSheets()[1];
var cell = sheets.getRange("b2");
cell.setFormula("=TRANSPOSE('Form Responses 1'!B1:AR1)");
var cell = sheets.getRange("c2");
cell.setFormula("=sheetName()");
var cell = sheets.getRange("c3");
cell.setFormula("=transpose(FILTER('Form Responses 1'!C:AR,'Form Responses 1'!B:B=C2))");
var cell = sheets.getRange("d5:d44");
cell.setFormula("=C5*vlookup(B5,'RG Cost'!B:E,4,0)");
var cell = sheets.getRange("d5:d44");
cell.setNumberFormat("[$₹][>9999999]##\,##\,##\,##0;[$₹][>99999]##\,##\,##0;[$₹]##,##0")
var cell = sheets.getRange("c47");
cell.setFormula("=sum(C5:C44)");
var cell = sheets.getRange("d47");
cell.setFormula("=sum(D5:D44)");
var cell = sheets.getRange("b47");
cell.setValue('TOTAL');
//Send active sheet as email attachment
var ssID = SpreadsheetApp.getActiveSpreadsheet();
var email = Session.getUser().getEmail();
var subject = "Order no.";
var body = "Hello";
var token = ScriptApp.getOAuthToken();
var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?" + "format=xlsx" + "&gid=" + "&portrait=true" + "&exportFormat=xlsx";
var result = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var contents = result.getContent();
MailApp.sendEmail("xyz@xyz.com",subject ,body, {attachments:[{fileName:colB_Data+".pdf", content:contents, mimeType:"application//pdf"}]});
};
答案 0 :(得分:1)
正如@cooper之前提到的那样,ssID没有正确添加到URL中,并且还有一些更改要设置url和你的fetchapp
//Send active sheet as email attachment
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetgId = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetId();
var email = Session.getUser().getEmail();
var subject = "Order no.";
var body = "Hello";
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?" + "format=xlsx" + "&gid="+sheetgId+ "&portrait=true" + "&exportFormat=pdf";
var result = UrlFetchApp.fetch(url)
var contents = result.getContent();
MailApp.sendEmail("abc@abc.abc",subject ,body, {attachments:[{fileName:colB_Data+".pdf", content:contents, mimeType:"application//pdf"}]});
您正在将HTTP GET直接发送到同一个驱动器,因此我认为用户已登录到Google帐户并可以访问电子表格,因此您不需要再次使用令牌。 其次,您忘记将变量添加到导出模式URL中的参数,并将导出格式更改为pdf。
答案 1 :(得分:0)
没有令牌就无法正常工作,所以我使用了它:
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ssID = ss.getId();
var sheetgId = ss.getActiveSheet().getSheetId();
var sheetName = ss.getName();
var token = ScriptApp.getOAuthToken();
var email = "name@domain.com";
var subject = "Important Info!";
var body = "PFA the report \n\nCheers,\n Roportobot";
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?" + "format=xlsx" + "&gid="+sheetgId+ "&portrait=true" + "&exportFormat=pdf";
var result = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var contents = result.getContent();
MailApp.sendEmail(email,subject ,body, {attachments:[{fileName:sheetName+".pdf", content:contents, mimeType:"application//pdf"}]});