Google Apps Script Configuration Guide
Table of Contents
- What is Google Apps Script?
- Apps Script Usage in the Project
- Audit Log Apps Script Configuration
- Image Insertion Apps Script Configuration
- Apps Script Execution API Configuration
- Service Account Permission Configuration
- Testing and Verification
What is Google Apps Script?
Google Apps Script is a JavaScript-based scripting platform provided by Google that can automate operations in Google Workspace applications (such as Google Sheets, Google Slides, Google Docs). In this project, Apps Script is used for:
- Audit Logging: Record user operations to Google Sheet
- Image Insertion: Insert generated images into Google Slides
Why Use Apps Script?
- Google Workspace Integration: Can directly operate Google Sheets and Google Slides without complex API calls
- Simplified Development: Uses familiar JavaScript language, simple to develop
- Execution Identity: Can execute as service account, ensuring permission control
- Cost-Effective: Google Apps Script is free for personal and enterprise users
Apps Script Usage in the Project
Audit Log Apps Script
Function: Record user operation logs to Google Sheet
Usage:
- User login/logout records
- Feature usage records (Market Opportunity Analysis, Audience Signal Analysis, Persona Slide)
- Result data records
- Export operation records
- User feedback records
File Location: docs/apps-script-code/AuditLogCode.gs
Image Insertion Apps Script
Function: Insert generated images into Google Slides
Usage:
- Insert analysis result charts into Google Slides template
- Replace placeholder text with actual images
- Insert images in blank slides
File Location: docs/apps-script-code/SlidePictureCode.gs
Note: These two Apps Script projects are completely independent and need to be created and configured separately.
Audit Log Apps Script Configuration
Step 1: Create Google Sheet
-
Create New Google Sheet
- Visit Google Sheets
- Create a new blank spreadsheet
- Name it
InsightHub Audit Log(or your chosen name)
-
Record Sheet ID
- Extract Sheet ID from URL
- URL format:
https://docs.google.com/spreadsheets/d/{SHEET_ID}/edit - For example: If URL is
https://docs.google.com/spreadsheets/d/1a2b3c4d5e6f7g8h9i0j/edit, then Sheet ID is1a2b3c4d5e6f7g8h9i0j
Step 2: Create Apps Script Project
-
Open Apps Script Editor
- Visit Google Apps Script
- Or visit
https://script.google.com/home
-
Create New Project
- Click "New Project"
- Project name:
InsightHub Audit Log
-
Copy Code
- Open the
docs/apps-script-code/AuditLogCode.gsfile in the project - Copy all content
- Paste into Apps Script editor's
Code.gsfile
- Open the
Step 3: Configure Script Properties
-
Open Project Settings
- Click "Project Settings" (gear icon) on the left
-
Add Script Properties
- In the "Script Properties" section, click "Add Script Property"
- Property:
SPREADSHEET_ID - Value: Sheet ID recorded in Step 1
- Click "Save Script Property"
Step 4: Record Script ID
-
Get Script ID
- In project settings page, find "Script ID"
- Or get from Apps Script editor URL
- URL format:
https://script.google.com/home/projects/{SCRIPT_ID}/edit
-
Configure Environment Variables
- (Deprecated) Previous versions used
APPS_SCRIPT_AUDIT_IDfor Apps Script–based logging. The current release writes directly to Google Sheets viaAUDIT_LOG_SPREADSHEET_ID, so this step can be skipped unless you are maintaining legacy deployments. - Development environment: Add to
.envfile - Production environment: Add to Cloud Build trigger substitution variables
- (Deprecated) Previous versions used
Step 5: Configure Sheet Permissions
- Share Sheet to Service Account
- In Google Sheet, click "Share" button in the top right
- Add service account email (format:
service-account-name@project-id.iam.gserviceaccount.com) - Set permission to "Editor"
- Click "Send"
Important: Service account must have Editor permission on the Sheet to write logs.
Image Insertion Apps Script Configuration
Step 1: Create Apps Script Project
-
Open Apps Script Editor
- Visit Google Apps Script
- Click "New Project"
-
Project Name
- Project name:
InsightHub Slide Picture
- Project name:
-
Copy Code
- Open the
docs/apps-script-code/SlidePictureCode.gsfile in the project - Copy all content
- Paste into Apps Script editor's
Code.gsfile
- Open the
Step 2: Record Script ID
-
Get Script ID
- In project settings page, find "Script ID"
- Or get from Apps Script editor URL
-
Configure Environment Variables
- Configure Script ID to environment variable
APPS_SCRIPT_SLIDES_ID - Development environment: Add to
.envfile - Production environment: Add to Cloud Build trigger substitution variables
- Configure Script ID to environment variable
Step 3: Configure Execution Identity
-
Open Project Settings
- Click "Project Settings" (gear icon) on the left
-
Execution Identity
- In the "Execution Identity" section, select "Me"
- This means Apps Script will execute as the caller's identity (usually service account)
Note: If using service account to call, Apps Script will execute as the service account, so the service account needs to have access permission to Google Slides.
Apps Script Execution API Configuration
What is Execution API?
Apps Script Execution API allows external applications (such as our backend server) to call functions in Apps Script through HTTP requests without deploying Apps Script as a Web App.
Enable Execution API
-
Open Google Cloud Console
- Visit Google Cloud Console
- Select your project
-
Enable Apps Script API
- Navigate to "APIs & Services" > "Library"
- Search for "Apps Script API"
- Click "Enable"
Configure Service Account Permissions
Service account needs to be able to call Apps Script Execution API:
- Grant Service Account Permission
- In Apps Script project, click "Share" button in the top right
- Add service account email
- Set permission to "Viewer" (sufficient)
- Click "Send"
Note:
- Service account only needs "Viewer" permission to call Execution API
- But when executing Apps Script, it will execute as the service account, so the service account needs to have actual operation permissions (such as Editor permission on Sheet, Editor permission on Slides)
Service Account Permission Configuration
Audit Log Service Account Permissions
Service account needs the following permissions:
-
Google Sheet Permission
- In audit log Sheet, add service account as "Editor"
- Service account email format:
service-account-name@project-id.iam.gserviceaccount.com
-
Apps Script Permission
- In audit log Apps Script project, add service account as "Viewer"
- This allows service account to call Execution API
Image Insertion Service Account Permissions
Service account needs the following permissions:
-
Google Slides Permission
- In Google Slides that need image insertion, add service account as "Editor"
- Or use Google Slides template, add service account as "Editor"
-
Apps Script Permission
- In image insertion Apps Script project, add service account as "Viewer"
- This allows service account to call Execution API
Permission Configuration Checklist
- Service account created
- Service account key downloaded
- Audit log Sheet shared to service account (Editor permission)
- Audit log Apps Script shared to service account (Viewer permission)
- Image insertion Apps Script shared to service account (Viewer permission)
- Google Slides template shared to service account (Editor permission, if needed)
Testing and Verification
Test Audit Log Apps Script
Method 1: Test in Apps Script Editor
-
Select Test Function
- In Apps Script editor, select a test function (e.g.,
testLogUserLogin) - Click "Run" button
- In Apps Script editor, select a test function (e.g.,
-
Authorize Permissions
- First run requires authorization
- Click "Authorize Access"
- Select your Google account
- Click "Allow"
-
View Results
- Check if logs have been written to Google Sheet
- View Apps Script execution logs, confirm no errors
Method 2: Test Through Backend API
-
Start Backend Server
cd server
npm run dev -
Call Test Endpoint (if exists)
- Or directly call audit log API endpoint
- Check if logs have been written to Sheet
Test Image Insertion Apps Script
Method 1: Test in Apps Script Editor
-
Prepare Test Data
- Create a test Google Slides
- Add placeholder text in slides (e.g.,
{{图片}}) - Prepare a Base64-encoded test image
-
Run Test Function
- In Apps Script editor, select
testInsertImagefunction - Modify function parameters (Presentation ID, placeholder, image data)
- Click "Run"
- In Apps Script editor, select
-
View Results
- Check if placeholder in Google Slides has been replaced with image
- View Apps Script execution logs, confirm no errors
Method 2: Test Through Backend API
- Call Image Insertion API
- Call image insertion function through backend API
- Check if image has been inserted in Google Slides
Verify Configuration
Checklist
- Apps Script project created
- Code correctly copied
- Script ID recorded and configured to environment variables
- Apps Script API enabled
- Service account permissions configured
- Test function runs successfully
- Logs/images correctly written/inserted
Frequently Asked Questions
Q1: Apps Script execution fails with insufficient permissions?
Error Message: PERMISSION_DENIED or You do not have permission to call this function
Possible Causes:
- Service account doesn't have access permission to Apps Script project
- Apps Script API not enabled
- Service account doesn't have actual operation permissions (such as Editor permission on Sheet)
Solution:
- In Apps Script project, add service account as "Viewer"
- Enable Apps Script API
- In Sheet/Slides, add service account as "Editor"
Q2: Audit log cannot write to Sheet?
Error Message: Exception: You do not have permission to perform that action
Possible Causes:
- Service account doesn't have Editor permission on Sheet
- Script ID configuration error
- Sheet ID configuration error
Solution:
- In Sheet, add service account as "Editor"
- (Legacy) Check if the deprecated
APPS_SCRIPT_AUDIT_IDvariable is still referenced anywhere in your deployment scripts. - Check if script property
SPREADSHEET_IDin Apps Script project is correct
Q3: How to get Script ID?
Method 1: Get from Project Settings
- In Apps Script editor, click "Project Settings" (gear icon)
- In "General" section, find "Script ID"
Method 2: Get from URL
- Apps Script editor URL format:
https://script.google.com/home/projects/{SCRIPT_ID}/edit - Extract
SCRIPT_IDpart from URL
Q4: Apps Script execution timeout?
Error Message: Execution timeout
Possible Causes:
- Apps Script execution time exceeds 6 minutes (free version limit)
- Data volume too large
- Network issues
Solution:
- Optimize Apps Script code to reduce execution time
- Process data in batches
- Check network connection
Q5: How to view Apps Script execution logs?
Method 1: View in Editor
- In Apps Script editor, click "Execute" (▶️) button
- View "Execution Log" panel
Method 2: View in Google Cloud Console
- Open Google Cloud Console
- Navigate to "Logs" > "Logs Explorer"
- Filter Apps Script related logs
Summary
This section detailed Google Apps Script configuration steps, including:
- Apps Script Overview: What is Apps Script, why use it
- Audit Log Configuration: How to create and configure audit log Apps Script
- Image Insertion Configuration: How to create and configure image insertion Apps Script
- Execution API Configuration: How to enable and configure Execution API
- Service Account Permissions: How to configure service account permissions
- Testing and Verification: How to test and verify configuration
Correctly configuring Apps Script is key to normal system function operation. Please complete the configuration step by step.
Related Documentation: