-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpublishing_googlesheet.js
190 lines (160 loc) · 5.89 KB
/
publishing_googlesheet.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const request = require('then-request');
const {google} = require('googleapis');
const {Storage} = require("@google-cloud/storage");
exports.publish_gsheet = async (req, res) => {
const DataprepJobID = req.body.jobid;
console.log("DataprepJobID : "+DataprepJobID);
const spreadsheetId = "1WiGd.........4tuoc";
const DataprepToken ="eyJhbGc........bcOwTQ";
// block on auth + getting the sheets API object
const auth = await google.auth.getClient({
scopes: [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/devstorage.read_only"
]
});
const sheetsAPI = google.sheets({version: 'v4',auth});
// ------------------ GET DATAPREP JOB AND CSV FILE NAME GENERATED IN GCS --------------------------------
const dataprep_job_endpoint = "https://api.clouddataprep.com/v4/jobGroups/"+DataprepJobID+"?embed=jobs.fileWriterJob.writeSetting";
var res_job = await request('GET', dataprep_job_endpoint, {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer '+ DataprepToken
},
});
const jsonresult = JSON.parse(res_job.getBody());
var outputFileURI=""
for (key in jsonresult.jobs.data) {
if (jsonresult.jobs.data[key].jobType == "filewriter") {
outputFileURI = jsonresult.jobs.data[key].writeSetting.path;
}
};
//gs://dataprep-staging-0b9ad034-9473-4777-98f1-0f3e643d0dce/[email protected]/jobrun/Sales_Data_small.csv
//console.log("outputFileURI : "+outputFileURI);
const outputFilepathArray = outputFileURI.split('/');
const outputBucket=outputFilepathArray[2];
console.log("Bucket : "+outputBucket);
var outputFilepath='';
for (key in outputFilepathArray) {
if (key > 2) {
outputFilepath = outputFilepath + outputFilepathArray[key]+'/';
}
};
outputFilepath=outputFilepath.slice(0,-1);
console.log("Output Filepath : "+outputFilepath);
const filename = outputFilepathArray.slice(-1).toString();
//console.log("Filename : "+filename);
const sheetName = filename.slice(0,-4)+"_"+DataprepJobID;
console.log("Sheet Name : "+sheetName);
const FileData = await readCSVContent(outputBucket,outputFilepath);
sheetid = await createEmptySheet(sheetName,spreadsheetId);
await populateAndStyle(FileData,sheetid,spreadsheetId);
res.send(`Spreadsheet ${sheetName} created`);
// ------------------ READ CSV FILE CONTENT FROM GCS --------------------------------
function readCSVContent(mybucket,myfilepath) {
return new Promise((resolve, reject) => {
const storage = new Storage();
const bucket = storage.bucket(mybucket);
const file = bucket.file(myfilepath);
let fileContents = Buffer.from('');
file.createReadStream()
.on('error', function(err) {
reject('The Storage API returned an error: ' + err);
})
.on('data', function(chunk) {
fileContents = Buffer.concat([fileContents, chunk]);
})
.on('end', function() {
let content = fileContents.toString('utf8');
//console.log("CSV content read as string : " + content );
resolve(content);
});
});
}
// ------------------ CREATE EMPTY NEW SHEET --------------------------------
function createEmptySheet(MySheetName,Myspreadsheetid) {
return new Promise((resolve, reject) => {
const emptySheetParams = {
spreadsheetId: Myspreadsheetid,
resource: {
requests: [
{
addSheet: {
properties: {
title: MySheetName,
index: 1,
gridProperties: {
rowCount: 10,
columnCount: 10,
frozenRowCount: 1
}
}
}
}
]
}
};
sheetsAPI.spreadsheets.batchUpdate( emptySheetParams, function(err, response) {
if (err) {
reject("The Sheets API returned an error: " + err);
} else {
const sheetId = response.data.replies[0].addSheet.properties.sheetId;
console.log("Created empty sheet: " + sheetId);
resolve(sheetId);
}
});
});
}
// ------------------ WRITE DATA IN THE NEW EMPTY SHEET --------------------------------
function populateAndStyle(FileData,MySheetId,MySpreadsheetId) {
return new Promise((resolve, reject) => {
// Using 'batchUpdate' allows for multiple 'requests' to be sent in a single batch.
// Populate the sheet referenced by its ID with the data received (a CSV string)
// Style: set first row font size to 11 and to Bold. Exercise left for the reader: resize columns
const dataAndStyle = {
spreadsheetId: MySpreadsheetId,
resource: {
requests: [
{
pasteData: {
coordinate: {
sheetId: MySheetId,
rowIndex: 0,
columnIndex: 0
},
data: FileData,
delimiter: ","
}
},
{
repeatCell: {
range: {
sheetId: MySheetId,
startRowIndex: 0,
endRowIndex: 1
},
cell: {
userEnteredFormat: {
textFormat: {
fontSize: 11,
bold: true
}
}
},
fields: "userEnteredFormat(textFormat)"
}
}
]
}
};
sheetsAPI.spreadsheets.batchUpdate(dataAndStyle, function(err, response) {
if (err) {
reject("The Sheets API returned an error: " + err);
} else {
console.log(MySheetId + " sheet populated with " + FileData.length + " rows and column style set.");
resolve();
}
});
});
}
}