Skip to content

Commit

Permalink
Tentacles v4.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
lushu committed Apr 19, 2024
1 parent cf0e5ad commit c5dc64d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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);
Expand Down

0 comments on commit c5dc64d

Please sign in to comment.