简体   繁体   English

从Google表格发送活动表格作为电子邮件附件

[英]Send the active sheet as an email attachment from google sheets

I have a google form that captures responses in a spreadsheet. 我有一个Google表单,可以捕获电子表格中的回复。 It currently creates a new sheet everytime a new response is made. 当前,每次做出新响应时,它都会创建一个新工作表。

I'm now trying to add a "mail the active sheet script" to the existing script that creates a new sheet. 我现在正在尝试向创建新工作表的现有脚本添加“通过邮件发送活动工作表脚本”。

However I get an error "Request failed for https://docs.google.com/spreadsheets/d/SS_ID/export ?" 但是我收到一条错误消息:“ https://docs.google.com/spreadsheets/d/SS_ID/export请求失败?” around the following 围绕以下

var result = UrlFetchApp.fetch(url, {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

I have used this article as a guide for the send email part. 我已将本文用作发送电子邮件部分的指南

This is the entire script I am using. 这是我正在使用的整个脚本。

    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"}]}); 

};

As mentioned by @cooper before, the ssID wasn't properly added to the URL, and also there's a few changes to be made the url and your fetchapp 就像@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"}]});

You're sending an HTTP GET directly to your same drive, so the user, I assume, is logged to a google account and has access to the spreadsheet, therefore you don't really need a token again. 您直接将HTTP GET发送到同一驱动器,因此,我认为该用户已登录到google帐户并可以访问电子表格,因此您实际上不需要再次使用令牌。 Second you forgot to add the variables to the parameters in the export mode URL and I changed the export format to pdf. 其次,您忘记将变量添加到导出模式URL中的参数中,而我将导出格式更改为pdf。

It didn't work without token so I used this: 没有令牌就无法正常工作,因此我使用了以下方法:

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"}]});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以将 Google 表格中的多张表格作为电子邮件附件发送? - Is it possible to send multiple sheets from Google Sheets as an email attachment? 如何将 Google 表格作为 email 附件发送? - How to send a Google Sheet as an email attachment? 从 Google 工作表发送电子邮件作为表格而不使用工作表转换器 - Send Email from Google sheet as a table without using sheets convertor 发送 PDF 活动表的附件与 Email 在同一活动表中的单元格参考 - Send PDF Attachment of Active Sheet with Email Cell Reference in Same Active Sheet Google 表格,如何向每位客户发送带有附件的电子邮件 - Google Sheets, how to send email with attachment to every customer 谷歌表格将格式从活动表格复制到其他所有表格 - google sheets copy formatting from active sheet to every other sheet 如何将附件从 email 提取到 google 表格中的共享驱动器? - How to extract an attachment from an email to a shared drive that is in a google sheets? 由于附件身份验证失败,Google sheet 脚本突然停止工作,无法发送 email 附件 - Google sheet script suddenly stops working and fails to send email attachment due to attachment authentication failure 与Google表格中的活动用户一起向多个用户发送电子邮件 - Send email to multiple user along with active user in Google Sheet Google 脚本 - 将活动工作表作为 PDF 发送到单元格中列出的 email [2019] - Google Script - Send active sheet as PDF to email listed in cell [2019]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM