-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscripts.js
More file actions
152 lines (127 loc) · 4.96 KB
/
scripts.js
File metadata and controls
152 lines (127 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
ensureScriptsTab,
getScriptsFromTab,
verifyScriptExists,
verifyAndHealScripts,
removeScriptFromTab,
removeMultipleScriptsFromTab,
addScriptToTab,
resolveScriptEntry
} from './scripts-helpers.js';
import { readScriptFiles, editScriptFile, writeScriptFile, searchScriptsOnDrive } from './scripts-api.js';
import { google } from 'googleapis';
export async function createScript(auth, sheetId, scriptName) {
const script = google.script({ version: 'v1', auth });
const response = await script.projects.create({
requestBody: {
title: scriptName,
parentId: sheetId
}
});
const scriptId = response.data.scriptId;
const url = `https://script.google.com/d/${scriptId}/edit`;
await addScriptToTab(auth, sheetId, scriptName, scriptId, url);
const exists = await verifyScriptExists(auth, scriptId);
if (!exists) {
throw new Error(`Script created but verification failed. Script ID: ${scriptId}`);
}
return { scriptId, name: scriptName, url };
}
export async function listScripts(auth, sheetId, options = {}) {
const { heal = true } = options;
await ensureScriptsTab(auth, sheetId);
const scripts = await getScriptsFromTab(auth, sheetId);
if (heal && scripts.length > 0) {
const result = await verifyAndHealScripts(auth, sheetId, scripts);
if (result.healed) {
return {
scripts: result.scripts.map((s, i) => ({
index: i, name: s.name, scriptId: s.scriptId, url: s.url, created: s.created
})),
healed: true, removedCount: result.removed.length
};
}
return { scripts: result.scripts, healed: false };
}
return { scripts, healed: false };
}
export async function readScript(auth, sheetId, scriptIdentifier) {
const scriptEntry = await resolveScriptEntry(auth, sheetId, scriptIdentifier);
const files = await readScriptFiles(auth, scriptEntry.scriptId);
return { scriptId: scriptEntry.scriptId, name: scriptEntry.name, files };
}
export async function editScript(auth, sheetId, scriptIdentifier, fileName, oldText, newText, replaceAll = false) {
const scriptEntry = await resolveScriptEntry(auth, sheetId, scriptIdentifier);
return editScriptFile(auth, scriptEntry.scriptId, fileName, oldText, newText, replaceAll);
}
export async function writeScript(auth, sheetId, scriptIdentifier, fileName, content, fileType = 'SERVER_JS') {
const scriptEntry = await resolveScriptEntry(auth, sheetId, scriptIdentifier);
return writeScriptFile(auth, scriptEntry.scriptId, fileName, content, fileType);
}
export async function deleteScript(auth, sheetId, scriptIdentifier) {
const scriptEntry = await resolveScriptEntry(auth, sheetId, scriptIdentifier, { verify: false });
const removed = await removeScriptFromTab(auth, sheetId, scriptEntry.scriptId);
if (!removed) {
throw new Error(`Script "${scriptEntry.name}" not found in tracking.`);
}
return {
deleted: true,
scriptId: scriptEntry.scriptId,
name: scriptEntry.name,
note: 'Removed from tracking. Apps Script projects cannot be deleted via API.'
};
}
export async function runScript(auth, sheetId, scriptIdentifier, functionName, parameters = []) {
const scriptEntry = await resolveScriptEntry(auth, sheetId, scriptIdentifier);
const script = google.script({ version: 'v1', auth });
let response;
try {
response = await script.scripts.run({
scriptId: scriptEntry.scriptId,
requestBody: {
function: functionName,
parameters,
devMode: true
}
});
} catch (e) {
if (e.code === 404 || e.message?.includes('not found')) {
await removeScriptFromTab(auth, sheetId, scriptEntry.scriptId);
throw new Error(`Script "${scriptEntry.name}" no longer exists. Tracking entry removed.`);
}
if (e.code === 403) {
throw new Error(`Script execution forbidden (403). Ensure you are the script owner — devMode only works for the owner. Non-owners need the script deployed as an API executable.`);
}
throw e;
}
if (response.data.error) {
const err = response.data.error;
throw new Error(`Script error: ${err.message || JSON.stringify(err)}`);
}
if (response.data.done === false) {
throw new Error(`Script execution did not complete (done=false). The script may have hit a quota limit or timeout.`);
}
return {
executed: true,
function: functionName,
result: response.data.response?.result ?? null
};
}
export async function syncScripts(auth, sheetId) {
await ensureScriptsTab(auth, sheetId);
const scripts = await getScriptsFromTab(auth, sheetId);
if (scripts.length === 0) {
return { synced: true, total: 0, valid: 0, removed: 0 };
}
const result = await verifyAndHealScripts(auth, sheetId, scripts);
return {
synced: true,
total: scripts.length,
valid: result.scripts.length,
removed: result.removed.length,
removedIds: result.removed
};
}
export async function searchScripts(auth, query, maxResults = 20) {
return searchScriptsOnDrive(auth, query, maxResults);
}