-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsheet-csv.gs
71 lines (59 loc) · 2.63 KB
/
gsheet-csv.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Google Apps Script */
/* Send Spreadsheet in an email as CSV & save to Drive, automatically */
function emailSpreadsheetAsCSV() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("NAME");
var lastRow = sheet.getLastRow();
Logger.log(lastRow);
var d = new Date();
var currentTime = d.toLocaleDateString();
Logger.log(currentTime)
// Send the CSV of the spreadsheet to this email address
var email = "[email protected]";
// Subject of email message
var subject = currentTime + " Orders for Supplier"
// Email Body can be HTML too with your logo image - see ctrlq.org/html-mail
var body = 'Dear Supplier, attached is a csv of our orders for the past day. ' +
'Please fufill and update the column Tracking_Number for each order and return the spreadsheet to us. ' +
'Additionally please let us know immediatley any items that are out of stock. ' +
'Thank you.';
// Base URL
var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
// Set config
var url_ext = 'exportFormat=csv&format=csv' // export as pdf / csv / xls / xlsx
+ '&size=letter' // paper size legal / letter / A4
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true&source=labnol' // fit to page width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&gid='; // the sheet's Id
// Auth
var token = ScriptApp.getOAuthToken();
// Fetch Spreadsheet
var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
headers: {
'Authorization': 'Bearer ' + token
}
});
// Format blob data to CSV
var blob = response.getBlob().setName(sheet.getName() + ' - FO ' + currentTime + '.csv');
// Save file to "Drive/Folder/Path"
var dir = DriveApp.getFolderById("FOLDERID");
var file = dir.createFile(blob);
// Send email with the CSV attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments:[blob],
from: "[email protected]",
name: "Company- Fulfilment",
replyTo: "[email protected]",
cc: "[email protected]"
});
// Cleanup Spreadsheet for processed orders
if (lastRow >= 2) {
// Purge rows that have been proccessed into blob
sheet.deleteRows(2, lastRow);
}
}