Skip to content

Commit fbb8e3b

Browse files
committed
fix: add file ID extraction error handling, use XmlService for robust HTML stripping, and update Gemini API parameter name.
1 parent a76a96e commit fbb8e3b

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

ai/autosummarize/summarize.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,18 @@ function summarizeFiles(
9393
},
9494
];
9595
const fileIdMatchPattern = /\/d\/(.*?)\//gi;
96-
const fileId = fileIdMatchPattern.exec(fileUrl)[1];
96+
const match = fileIdMatchPattern.exec(fileUrl);
97+
if (!match) {
98+
console.log(`Could not extract file ID from URL: ${fileUrl}`);
99+
return [
100+
fileUrl,
101+
fileName,
102+
"Could not extract file ID from URL.",
103+
"",
104+
"",
105+
];
106+
}
107+
const fileId = match[1];
97108

98109
// Get file title and type.
99110
const currentFile = Drive.Files.get(fileId, { supportsAllDrives: true });
@@ -139,7 +150,7 @@ function summarizeFiles(
139150
// Prompt for summary
140151
const geminiOptions = {
141152
temperature,
142-
tokens,
153+
maxOutputTokens: tokens,
143154
};
144155
summary = getAiSummary(promptParts, geminiOptions);
145156

ai/custom-func-ai-agent/Code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
DEFAULT_OUTPUT_FORMAT =
17+
const DEFAULT_OUTPUT_FORMAT =
1818
"Summarize it. Only keep the verdict result and main arguments. " +
1919
"Do not reiterate the fact being checked. Remove all markdown. " +
2020
"State the verdit result in a first paragraph in a few words and " +

ai/devdocs-link-preview/Vertex.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const MODEL_ID = scriptPropertyWithDefault("model_id", "gemini-2.5-flash");
2222
const SERVICE_ACCOUNT_KEY = scriptPropertyWithDefault("service_account_key");
2323

2424
/**
25-
* Invokes Gemini to extrac the title and summary of a given URL. Responses may be cached.
25+
* Invokes Gemini to extract the title and summary of a given URL. Responses may be cached.
2626
*/
2727
function getPageSummary(targetUrl) {
2828
const cachedResponse = CacheService.getScriptCache().get(targetUrl);
@@ -84,8 +84,9 @@ function getPageSummary(targetUrl) {
8484
if (!jsonMatch) {
8585
throw new Error("Unable to generate preview,");
8686
}
87-
CacheService.getScriptCache().put(targetUrl, jsonMatch);
88-
return JSON.parse(jsonMatch[0]);
87+
const jsonResponse = jsonMatch[0];
88+
CacheService.getScriptCache().put(targetUrl, jsonResponse);
89+
return JSON.parse(jsonResponse);
8990
}
9091

9192
/**

solutions/custom-functions/calculate-driving-distance/Code.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ function generateStepByStep_() {
167167
const newRows = [];
168168
for (const step of directions.routes[0].legs[0].steps) {
169169
// Remove HTML tags from the instructions.
170-
const instructions = step.html_instructions
171-
.replace(/<br>|<div.*?>/g, "\n")
172-
.replace(/<.*?>/g, "");
170+
const instructions = XmlService.parse(
171+
`<root>${step.html_instructions}</root>`,
172+
)
173+
.getRootElement()
174+
.getText();
173175
newRows.push([instructions, step.distance.value]);
174176
}
175177
directionsSheet.getRange(1, 1, headers.length, 3).setValues(headers);

0 commit comments

Comments
 (0)