Skip to content

feat: add inlineData parameter to SSI custom function (deferred — Drive auth blocked)#20

Open
aaronbrezel wants to merge 4 commits into
developfrom
feature/ssi-inline-data
Open

feat: add inlineData parameter to SSI custom function (deferred — Drive auth blocked)#20
aaronbrezel wants to merge 4 commits into
developfrom
feature/ssi-inline-data

Conversation

@aaronbrezel

Copy link
Copy Markdown
Member

Summary

Deferred continuation of #18. Adds a fourth inlineData parameter to SSI() so users can attach Drive files as inline data parts in a cell formula.

Status: blocked. ScriptApp.getOAuthToken() in AuthMode.CUSTOM_FUNCTION context returns a token scoped only to spreadsheets.currentonly — Drive API calls return 401. This PR captures the investigation and a workable starting point for a proper solution.

Changes on this branch (on top of develop)

  • src/server/drive.ts: fetchAndEncodeFile replaced with a Drive REST API implementation via UrlFetchApp + ScriptApp.getOAuthToken(), replacing the DriveApp version that is unavailable in custom function context. Includes a null-token guard that returns an actionable error message.
  • src/server/customFunctions.ts: inlineData? parameter restored; fetchAndEncodeFile and extractId imports re-added; resolvedInlineData block re-added.
  • rollup.config.js: SSI footer stub updated back to 4-parameter signature.
  • CLAUDE.md: AuthMode.CUSTOM_FUNCTION OAuth scope limitation documented.

Why it's blocked

Custom functions in a bound Apps Script run with AuthMode.CUSTOM_FUNCTION. The OAuth token available via ScriptApp.getOAuthToken() in this context has only spreadsheets.currentonly scope — it cannot authorize Drive API requests. This is a GAS platform constraint for bound scripts.

Possible paths forward

  1. Service account — share files with a service account email, store the key in Script Properties. Practical only if the file set is controlled.
  2. Web app proxy — deploy a second Apps Script as a web app with ANYONE or DOMAIN access; call it from the custom function via UrlFetchApp. The web app runs with full auth and can return the encoded file.
  3. Abandon inlineData on SSI — remove the parameter from the custom function entirely; Drive file support remains menu-only via Run AI.

Test plan

  • npm run test:coverage — all tests pass, all thresholds met (71 tests)
  • npm run typecheck — no errors
  • npm run build — SSI stub confirmed in dist/index.js with 4-parameter signature
  • Resolve Drive auth limitation before deploying

🤖 Generated with Claude Code

aaronbrezel and others added 4 commits February 23, 2026 14:44
DriveApp is unavailable in Google Apps Script custom function execution
contexts, causing "permissions not sufficient" errors when SSI() is
called with an inlineData argument. UrlFetchApp and ScriptApp.getOAuthToken()
work in all contexts and are the GAS-documented pattern for this case.

fetchAndEncodeFile now makes two Drive REST API calls via UrlFetchApp:
one for file metadata (mimeType + size) and one for file content.
Error responses from each call surface a descriptive message.

Update drive.test.ts to mock the Drive REST API calls instead of DriveApp,
and add fallback-message tests to restore 100% branch coverage on drive.ts.
Update customFunctions.test.ts to use mockDriveApiFile() helper and a
getGeminiPayload() helper that locates the Gemini call by URL regardless
of how many Drive calls precede it.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Custom functions cannot use DriveApp or other OAuth-requiring services.
Document the UrlFetchApp + ScriptApp.getOAuthToken() pattern as the
correct alternative, with a pointer to fetchAndEncodeFile as an example.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Custom functions run in AuthMode.CUSTOM_FUNCTION, which scopes
ScriptApp.getOAuthToken() to spreadsheets.currentonly only. Drive API
calls with this token return 401 — not a permissions error, but an auth
scope mismatch that cannot be resolved without a service account.

- fetchAndEncodeFile: guard for null/insufficient token with a clear
  error pointing users to the Run AI menu tool
- customFunctions.ts: revert the auth-error heuristic (no longer needed
  now that the error surfaces directly from fetchAndEncodeFile)
- drive.ts / CLAUDE.md: accurately document the AuthMode.CUSTOM_FUNCTION
  constraint, why ScriptApp.getOAuthToken() doesn't help, and the
  service-account alternative with its UX trade-off
- drive.test.ts: update null-token test assertion to match new message

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The cleanup commit on feature/ssi-custom-function removed inlineData from
the footer stub, but feature/ssi-inline-data re-adds it to customFunctions.ts.
The stub was out of sync, silently dropping the inlineData argument.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@aaronbrezel aaronbrezel added bug Something isn't working enhancement New feature or request labels Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant