-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnightlyscript.gs
105 lines (93 loc) · 3.5 KB
/
nightlyscript.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Holds unique vendor information for Indira Active's suppliers.
* @param {String} name References unofficial internal name
* @param {String} spreadsheetID Google Spreadsheet id from vendor product mapping & inventory
* @param {String} sheetName Google Sheet name with current inventory
* @param {String} inventoryEmail Sparkshipping import address for vendor.
*/
function vendor(name, spreadsheetID, sheetName, inventoryEmail) {
this.name = name;
this.spreadsheetID = spreadsheetID;
this.sheetName = sheetName;
this.inventoryEmail = inventoryEmail;
}
var config = {
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
dir: DriveApp.getFolderById("UNIQUE_DRIVE_FOLDER_ID"),
currentTime: new Date().toLocaleString(),
folder: ""
};
/**
* Initalizes script and vendors.
*/
function init() {
// Drive create folder with date
config.folder = config.dir.createFolder('Inventory as of ' + config.currentTime);
vendors = [];
// Initializ Suppliers
var ardenFulfillment = new vendor('Arden Fulfillment', 'UNIQUE_SPREADSHEET_ID', 'Internal Inventory', 'INVENTORY_SYSTEM_EMAIL_ADDRESS');
vendors.push(ardenFulfillment);
}
/**
* Retrieves and formats inventory from Google Sheet.
* @param {Object} vendor Holds unique vendor information
* @returns {Object} blob Binary CSV export of spreadsheet
*/
function prepData(vendor) {
var ss = SpreadsheetApp.openById(vendor.spreadsheetID);
Logger.log(ss.getName());
var sheet = ss.getSheetByName(vendor.sheetName);
var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
var token = ScriptApp.getOAuthToken();
// Fetch Spreadsheet
var response = UrlFetchApp.fetch(url + config.url_ext + sheet.getSheetId(), {
headers: {
'Authorization': 'Bearer ' + token
}
});
// Format blob data to CSV and save in Drive
var blob = response.getBlob().setName(vendor.name + ' inventory as of ' + config.currentTime + '.csv');
var file = config.folder.createFile(blob);
return blob;
}
/**
* Sends inventory levels to inventory system.
* @param {Object} vendor Holds unique vendor information
* @param {Object} blob Binary CSV export of spreadsheet
*/
function syncInventory(vendor, blob) {
var subject = vendor.name + " inventory as of " + config.currentTime;
var body = 'Automatic inventory import to Sparkshipping.';
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(vendor.inventoryEmail, subject, body, {
attachments: [blob],
noReply: true,
name: "Indira Active - Inventory",
bcc: "[email protected]"
});
Logger.log("Sent email to " + vendor.inventoryEmail + " for " + vendor.name + ".");
}
/**
* Runs the Inventory Import script.
*/
function batch() {
init();
for (var i = 0; i < vendors.length; i++) {
inventory = prepData(vendors[i]);
syncInventory(vendors[i], inventory);
}
}