Skip to content

add google docs with actions and triggers and update app locale setup #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ env:
GOOGLE_MEET_CLIENT_SECRET: ${{ secrets.GOOGLE_MEET_CLIENT_SECRET }}
GOOGLE_MEET_CLIENT_ID: ${{ secrets.GOOGLE_MEET_CLIENT_ID }}
GOOGLE_MEET_REFRESH_TOKEN: ${{ secrets.GOOGLE_MEET_REFRESH_TOKEN }}
GOOGLE_DOCS_CLIENT_SECRET: ${{ secrets.GOOGLE_DOCS_CLIENT_SECRET }}
GOOGLE_DOCS_CLIENT_ID: ${{ secrets.GOOGLE_DOCS_CLIENT_ID }}
GOOGLE_DOCS_REFRESH_TOKEN: ${{ secrets.GOOGLE_DOCS_REFRESH_TOKEN }}

jobs:
PullRequestTests:
Expand Down
1 change: 1 addition & 0 deletions ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"author": "Qore Technologies s.r.o.",
"license": "MIT",
"dependencies": {
"@googleapis/docs": "^3.4.0",
"@googleapis/drive": "^12.0.0",
"@googleapis/forms": "^2.5.0",
"@googleapis/meet": "^0.1.1",
Expand Down
2 changes: 2 additions & 0 deletions ts/src/ActionsCatalogue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { PiecesAppCatalogue } from '../pieces/piecesCatalogue';
import { Debugger, DebugLevels } from '../utils/Debugger';
import googleForms from '../apps/google-forms';
import googleMeet from '../apps/google-meet';
import googleDocs from '../apps/google-docs';

if (process.env.TS_DEBUG) {
Debugger.level = DebugLevels.Verbose;
Expand All @@ -53,6 +54,7 @@ export interface IQoreApi {
}

const NEW_APPS = {
googleDocs,
googleMeet,
googleSheets,
googleDrive,
Expand Down
260 changes: 260 additions & 0 deletions ts/src/apps/google-docs/actions/append-text-to-file.action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
import {
EQoreAppActionCode,
QoreAppCreator,
TQoreOptions,
TQoreResponseType,
} from '@qoretechnologies/ts-toolkit';
import { getQoreContextRequiredValues } from '../../../global/helpers';
import { GOOGLE_DOCS_APP_NAME, GoogleDocsError } from '../constants';
import { createGoogleDocsClient } from '../helpers/constants';
import { getGoogleDocsDocumentIdAllowedValues } from '../helpers/get-document-id-allowed-values';

const options = {
document_id: {
type: 'string',
required: true,
allowed_values_creatable: true,
get_allowed_values: getGoogleDocsDocumentIdAllowedValues,
},
text: {
type: 'string',
required: true,
},
insert_position: {
type: 'string',
required: false,
default_value: 'end',
allowed_values: [
{
display_name: 'End of Document',
value: 'end',
},
{
display_name: 'Beginning of Document',
value: 'beginning',
},
{
display_name: 'Specific Index',
value: 'index',
},
],
},
index: {
type: 'integer',
required: false,
},
add_line_break: {
type: 'boolean',
required: false,
default_value: true,
},
text_style: {
type: {
type: 'hash',
fields: {
bold: {
type: 'boolean',
required: false,
},
italic: {
type: 'boolean',
required: false,
},
underline: {
type: 'boolean',
required: false,
},
font_size: {
type: 'integer',
required: false,
},
font_family: {
type: 'string',
required: false,
},
},
},
required: false,
},
} satisfies TQoreOptions;

const response_type = {
type: 'hash',
fields: {
document_id: {
type: 'string',
},
revision_id: {
type: 'string',
},
inserted_text: {
type: 'string',
},
insert_index: {
type: 'integer',
},
success: {
type: 'boolean',
},
message: {
type: 'string',
},
},
} satisfies TQoreResponseType;

export const appendTextToGoogleDocsDocument = QoreAppCreator.createLocalizedAction<typeof options>({
app: GOOGLE_DOCS_APP_NAME,
action: 'append_text_to_document',
action_code: EQoreAppActionCode.ACTION,
options,
response_type,
api_function: async (data, _opts, context) => {
const { token, document_id, text } = getQoreContextRequiredValues({
context: { ...context, opts: data },
optionFields: ['document_id', 'text'],
connectionFields: ['token'],
ErrorClass: GoogleDocsError,
});

const insert_position = data?.insert_position;
const index = data?.index;
const add_line_break = data?.add_line_break;
const text_style = data?.text_style;

try {
const docsClient = createGoogleDocsClient(token);

const documentResponse = await docsClient.documents.get({
documentId: document_id,
});

if (!documentResponse.data) {
throw new GoogleDocsError('Failed to retrieve document information');
}

const document = documentResponse.data;
const bodyContent = document.body?.content;

if (!bodyContent) {
throw new GoogleDocsError('Document has no content body');
}

let insertIndex: number;

if (insert_position === 'beginning') {
insertIndex = 1;
} else if (insert_position === 'index' && typeof index === 'number') {
insertIndex = Math.max(1, index);
} else {
let endIndex = 1;
for (const element of bodyContent) {
if (element.endIndex && element.endIndex > endIndex) {
endIndex = element.endIndex;
}
}
insertIndex = endIndex - 1;
}

let textToInsert = text;
if (add_line_break && insert_position !== 'beginning') {
textToInsert = '\n' + text;
} else if (add_line_break && insert_position === 'beginning') {
textToInsert = text + '\n';
}

const requests: any[] = [];

requests.push({
insertText: {
location: {
index: insertIndex,
},
text: textToInsert,
},
});

if (text_style && Object.keys(text_style).length > 0) {
const textStyleRequest: any = {
updateTextStyle: {
range: {
startIndex: insertIndex,
endIndex: insertIndex + textToInsert.length,
},
textStyle: {},
fields: '',
},
};

const styleFields: string[] = [];

if (text_style.bold !== undefined) {
textStyleRequest.updateTextStyle.textStyle.bold = text_style.bold;
styleFields.push('bold');
}

if (text_style.italic !== undefined) {
textStyleRequest.updateTextStyle.textStyle.italic = text_style.italic;
styleFields.push('italic');
}

if (text_style.underline !== undefined) {
textStyleRequest.updateTextStyle.textStyle.underline = text_style.underline;
styleFields.push('underline');
}

if (text_style.font_size !== undefined) {
textStyleRequest.updateTextStyle.textStyle.fontSize = {
magnitude: text_style.font_size,
unit: 'PT',
};
styleFields.push('fontSize');
}

if (text_style.font_family) {
textStyleRequest.updateTextStyle.textStyle.weightedFontFamily = {
fontFamily: text_style.font_family,
};
styleFields.push('weightedFontFamily');
}

if (styleFields.length > 0) {
textStyleRequest.updateTextStyle.fields = styleFields.join(',');
requests.push(textStyleRequest);
}
}

const updateResponse = await docsClient.documents.batchUpdate({
documentId: document_id,
requestBody: {
requests,
},
});

return {
document_id,
revision_id: updateResponse.data.documentId || document_id,
inserted_text: textToInsert,
insert_index: insertIndex,
success: true,
message: `Successfully appended text to document ${document_id}`,
};
} catch (error: any) {
if (error instanceof GoogleDocsError) {
throw error;
}

if (error.response?.data?.error) {
const apiError = error.response.data.error;
throw new GoogleDocsError(
`Google Docs API error: ${apiError.message || 'Unknown error'} (Code: ${apiError.code || 'unknown'})`
);
}

throw new GoogleDocsError(
`Failed to append text to document: ${error.message || 'Unknown error'}`
);
}
},
});

export default appendTextToGoogleDocsDocument;
Loading
Loading