簡體   English   中英

"以 CSV 格式郵寄 Google 表格"

[英]Mailing Google Sheet as CSV

只是嘗試獲取當前的電子表格選項卡並將其作為 *.csv 文件郵寄到一個地址。 我已經經歷了很多先前的問題,但還沒有找到答案。 這改編自成功發送 PDF 文件。 唯一的問題是我需要 CSV 而不是 PDF。

function mailCSV() {
// Get the currently active spreadsheet URL (link)
// Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
// Send the CSV of the spreadsheet to this email address
   var email_send_address = "email@emailsample.com"; 
   var email_subject ='subject'
// Get the currently active spreadsheet URL (link)
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var url = "http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+ ss + "&exportFormat=csv"; 

//var csv = DriveApp.getFileById(ss.getId()).getAs('application/csv').getBytes();
//var attachCSV = {fileName: email_subject,content:url,mimeType:'application/csv'};

  var res = UrlFetchApp.fetch(url);
  var attachments = [{fileName:"dg_request.csv", content: res.getContent(),mimeType:"application/vnd.csv"}];

MailApp.sendEmail(email_send_address, email_subject, {attachments: attachments});

// MailApp.sendEmail(email_send_address, email_subject, {attachments:[attachCSV]});
}

好吧,一堆亂七八糟的東西,我終於偶然發現了一個簡單且有效的解決方案。 (向您展示即使是盲豬也能找到玉米穗)。 請注意,它不會發送文件,而是實際上將 csv 作為消息正文發送......這對我來說很好。

function mailRequestCSV() {

// Send the PDF of the spreadsheet to this email address
  var email_send_address = "david.fickes@sunrunhome.com"; 
  var email_subject = "datagen request"

// Get the currently active spreadsheet ID
  var fileID = SpreadsheetApp.getActiveSpreadsheet();

// Build the URL 
  var url = 'https://docs.google.com/spreadsheet/ccc?key='+fileID+'&output=csv';
  var response = UrlFetchApp.fetch(url);

  MailApp.sendEmail(email_send_address, email_subject, response);
}

作為附件發送的更好的答案:

完全歸功於: https : //gist.github.com/nirajkadam/0b41a01b8e739800c964

function mailRequestCSV() {

// Send the PDF of the spreadsheet to this email address
var recipients = "someone@somewhere.com"; 
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
var email = Session.getActiveUser().getEmail();
var subject = "DataGenerationRequest ";
var body = "****NOTE: THIS IS AN AUTO COMPUTER-GENERATED REPORT****";
var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?format=csvx&id="+ssID;
var result = UrlFetchApp.fetch(url, requestData);
var contents = result.getContent();

MailApp.sendEmail(recipients, subject, body, {attachments:[{fileName:sheetName+".csv", content:contents, mimeType:"application//csv"}]});
}

2022 年 1 月嘗試過,但對我不起作用。

我的解決方案是簡單地將 URL 格式更改<\/strong>為:

var url = 'https://docs.google.com/spreadsheets/d/' + SpreadSheetId + "/export?format=csv&gid=" + SheetId;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM