diff --git a/marketing-analytics/activation/common-libs/nodejs-common/package.json b/marketing-analytics/activation/common-libs/nodejs-common/package.json index 803ac8f..c76420e 100644 --- a/marketing-analytics/activation/common-libs/nodejs-common/package.json +++ b/marketing-analytics/activation/common-libs/nodejs-common/package.json @@ -1,6 +1,6 @@ { "name": "@google-cloud/nodejs-common", - "version": "2.2.1", + "version": "2.2.2", "description": "A NodeJs common library for solutions based on Cloud Functions", "author": "Google Inc.", "license": "Apache-2.0", diff --git a/marketing-analytics/activation/common-libs/nodejs-common/src/apis/spreadsheets.js b/marketing-analytics/activation/common-libs/nodejs-common/src/apis/spreadsheets.js index e439f92..12b7faa 100644 --- a/marketing-analytics/activation/common-libs/nodejs-common/src/apis/spreadsheets.js +++ b/marketing-analytics/activation/common-libs/nodejs-common/src/apis/spreadsheets.js @@ -109,21 +109,73 @@ class Spreadsheets extends GoogleApiClient { } /** - * Clears the content of the Sheet. - * see: - * https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear + * Returns whether the sheet with speicified name exists. + * @param {string} sheetName + * @return {boolean} + */ + async hasSheet(sheetName) { + const sheets = await this.getApiClient(); + const spreadsheet = await sheets.spreadsheets.get( + { spreadsheetId: this.spreadsheetId }); + return spreadsheet.data.sheets.some( + ({ properties: { title } }) => title === sheetName); + } + + /** + * Creates a sheet with the gvien name. + * + * @param {string} sheetName + */ + async createSheet(sheetName) { + const sheets = await this.getApiClient(); + await sheets.spreadsheets.batchUpdate({ + spreadsheetId: this.spreadsheetId, + requestBody: { + requests: [{ addSheet: { properties: { title: sheetName } } }], + } + }); + } + + /** + * Deletes a sheet with the gvien name. + * + * @param {string} sheetName + */ + async deleteSheet(sheetName) { + if (await this.hasSheet(sheetName)) { + const sheetId = await this.getSheetId(sheetName); + const sheets = await this.getApiClient(); + await sheets.spreadsheets.batchUpdate({ + spreadsheetId: this.spreadsheetId, + requestBody: { + requests: [{ deleteSheet: { sheetId } }], + } + }); + } + } + + /** + * Clears the content of the Sheet. If the Sheet doesn't exist, then creates + * an empty sheet with the sheetName.. + * @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear * @param {string} sheetName Name of the Sheet. */ async clearSheet(sheetName) { - const request = { - spreadsheetId: this.spreadsheetId, - range: sheetName, - }; try { - const sheets = await this.getApiClient(); - const response = await sheets.spreadsheets.values.clear(request); - const data = response.data; - this.logger.debug(`Clear sheet[${sheetName}}]: `, data); + const hasSheet = await this.hasSheet(sheetName); + if (!hasSheet) { + await this.createSheet(sheetName); + this.logger.debug(`Create sheet[${sheetName}]`); + } else { + const sheets = await this.getApiClient(); + const request = { + spreadsheetId: this.spreadsheetId, + range: sheetName, + }; + const response = await sheets.spreadsheets.values.clear(request); + const data = response.data; + this.logger.debug(`Clear sheet[${sheetName}]: `, data); + } } catch (error) { this.logger.error(error.toString()); throw error; diff --git a/marketing-analytics/activation/gmp-googleads-connector/package.json b/marketing-analytics/activation/gmp-googleads-connector/package.json index fec1a7a..ebb89ee 100644 --- a/marketing-analytics/activation/gmp-googleads-connector/package.json +++ b/marketing-analytics/activation/gmp-googleads-connector/package.json @@ -1,6 +1,6 @@ { "name": "@google-cloud/gmp-googleads-connector", - "version": "4.4.1", + "version": "4.4.2", "description": "GMP & Google Ads connector based on Cloud Functions", "author": "Google Inc.", "license": "Apache-2.0", @@ -20,7 +20,7 @@ "homepage": "https://github.com/GoogleCloudPlatform/cloud-for-marketing/blob/master/marketing-analytics/activation/gmp-googleads-connector/README.md", "main": "index.js", "dependencies": { - "@google-cloud/nodejs-common": "^2.2.1", + "@google-cloud/nodejs-common": "^2.2.2", "@google-cloud/storage": "^7.9.0", "lodash": "^4.17.21", "nanoid": "^3.3.4", diff --git a/marketing-analytics/activation/gmp-googleads-connector/src/api_handlers/sheets_load_csv.js b/marketing-analytics/activation/gmp-googleads-connector/src/api_handlers/sheets_load_csv.js index 757b98d..2294ac9 100644 --- a/marketing-analytics/activation/gmp-googleads-connector/src/api_handlers/sheets_load_csv.js +++ b/marketing-analytics/activation/gmp-googleads-connector/src/api_handlers/sheets_load_csv.js @@ -48,7 +48,8 @@ const DEFAULT_PASTE_TYPE = 'PASTE_NORMAL'; * https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#pastedatarequest * * @typedef {{ -* spreadsheetId:string, +* spreadsheetId:(string|undefined), +* spreadsheetUrl:(string|undefined}, * sheetName:string, * sheetHeader:(string|undefined), * requestLength:(number|undefined), @@ -75,6 +76,10 @@ class GoogleSheetLoadCsv extends ApiHandler { * @override */ sendData(message, messageId, config) { + if (!config.spreadsheetId && config.spreadsheetUrl) { + const id = /spreadsheets\/d\/([^\/]*)\//i.exec(config.spreadsheetUrl); + if (id) config.spreadsheetId = id[1]; + } const spreadsheets = new Spreadsheets( config.spreadsheetId, this.getOption(config)); return this.sendDataInternal(spreadsheets, message, messageId, config);