Skip to content

Commit f0c40ab

Browse files
Merge pull request #7 from ScriptAddicts/FPR-886
FPR-886 Clean up temp files in case of inserting sheets failure
2 parents 02d0b04 + d526afe commit f0c40ab

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@scriptaddicts/docsserviceapp",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "Fork of DocsServiceApp to continue support of the library",
55
"main": "index.js",
66
"scripts": {

src/apps/SpreadsheetApp/SpreadsheetAppp.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,28 @@
165165
putError.call(this, e.message);
166166
}
167167
}
168-
tmpSheet = SpreadsheetApp.openById(tmpId)
169-
.getSheets()[0]
170-
.setName(`SpreadsheetAppp_${Utilities.getUuid()}`)
171-
.copyTo(dstSS);
172-
Drive.Files.remove(tmpId);
168+
try {
169+
const tmpSpreadsheet = SpreadsheetAppp.openById(tmpId);
170+
tmpSheet = tmpSpreadsheet.getSheets()[0];
171+
tmpSheet.setName(`SpreadsheetAppp_${Utilities.getUuid()}`);
172+
tmpSheet.copyTo(dstSS);
173+
174+
Drive.Files.remove(tmpId);
175+
} catch (err) {
176+
// in case of failure remove Temp generated file to avoid spamming
177+
console.log(
178+
"[DocsServiceApp]",
179+
"Error copying tmp file into destination",
180+
err.name,
181+
err.message
182+
);
183+
184+
if (tmpId) {
185+
Drive.Files.remove(tmpId);
186+
}
187+
188+
throw err;
189+
}
173190
tmpSheetId = tmpSheet.getSheetId();
174191
requests = ar.map((e) => {
175192
e.from.sheetId = tmpSheetId;
@@ -249,22 +266,31 @@
249266
gToM = function () {
250267
var obj, url;
251268
url = `https://www.googleapis.com/drive/v3/files/${this.obj.spreadsheetId}/export?mimeType=${MimeType.MICROSOFT_EXCEL}`;
252-
console.log(
253-
"[FPR-859] Requesting MS export",
254-
"Library object ->",
255-
JSON.stringify(this.obj || {}),
256-
"Request URL ->",
257-
url
258-
)
259-
try {
260-
console.log("[FPR-859] Request headers -> ", JSON.stringify(this.headers || {}))
261-
obj = UrlFetchApp.fetch(url, {
262-
headers: this.headers,
263-
});
264-
console.log("[FPR-859] Successfull MS file export response ->", obj.getResponseCode())
265-
} catch (err) {
266-
console.log("[FPR-859] Errored MS file export response -> ", obj.getResponseCode())
267-
}
269+
console.log(
270+
"[FPR-859] Requesting MS export",
271+
"Library object ->",
272+
JSON.stringify(this.obj || {}),
273+
"Request URL ->",
274+
url
275+
);
276+
try {
277+
console.log(
278+
"[FPR-859] Request headers -> ",
279+
JSON.stringify(this.headers || {})
280+
);
281+
obj = UrlFetchApp.fetch(url, {
282+
headers: this.headers,
283+
});
284+
console.log(
285+
"[FPR-859] Successfull MS file export response ->",
286+
obj.getResponseCode()
287+
);
288+
} catch (err) {
289+
console.log(
290+
"[FPR-859] Errored MS file export response -> ",
291+
obj.getResponseCode()
292+
);
293+
}
268294
if (obj.getResponseCode() !== 200) {
269295
putError.call(
270296
this,

0 commit comments

Comments
 (0)