メインコンテンツにスキップ

GeminiWithFiles

This is a Google Apps Script library for Gemini API with files.

AI Summary

GeminiWithFiles

GAS library for easy file upload to Gemini API and content generation from files.

Target Users

Intermediate GAS developers who want to process images/PDFs with Gemini API for descriptions/parsing.

Problems Solved

In GAS, Gemini API lacks direct support for uploading/processing multiple Drive files like images/PDFs.

Script ID

  1. In GAS Editor: Click "Libraries +" → Paste into "Script ID" field → Click "Look up"
  2. "GeminiWithFiles" will appear in the search results
  3. Select the latest version (highest number) from "Version" dropdown
  4. Click "Add"

Tags

Main Features
1
Easy File Upload

Set Drive file IDs/blobs with setFileIds/setBlobs and upload asynchronously to Gemini. PDFs directly supported.

2
Multi-File Processing

Use withUploadedFilesByGenerateContent to process multiple images/PDFs in one generateContent call.

3
Output Control

Control output with response_mime_type/JSON schema for structured JSON responses.

Examples

Main Functions

FunctionDescription
GeminiWithFiles.geminiWithFilesCreate Gemini instance with API key
setFileIdsSet Drive file IDs for upload
generateContentGenerate content using files

Examples

Basic Instance Creation and Generation
/**
 * 基本的なインスタンス作成と生成
 * ・APIキー指定: GeminiWithFiles.geminiWithFiles({ apiKey })
 * ・テキストクエリ生成: generateContent({ q: "..." })
 * ・結果ログ出力: console.log(res)
 */
function myFunction() {
  const apiKey = "###";
  const g = GeminiWithFiles.geminiWithFiles({ apiKey });
  const res = g.generateContent({ q: "What is Google Apps Script?" });
  console.log(res);
}

File Upload and Multi-File Processing
/**
 * ファイルアップロードと複数処理
 * ・ファイルID取得: DriveApp.getFolderById(folderId).getFiles()
 * ・ID設定とアップロード: setFileIds(fileIds, false).uploadFiles()
 * ・ファイル付き生成: withUploadedFilesByGenerateContent(fileList).generateContent({ q })
 */
function myFunction() {
  const apiKey = "###";
  const folderId = "###";
  let fileIds = [];
  const files = DriveApp.getFolderById(folderId).getFiles();
  while (files.hasNext()) {
    fileIds.push(files.next().getId());
  }
  const g = GeminiWithFiles.geminiWithFiles({ apiKey });
  const fileList = g.setFileIds(fileIds, false).uploadFiles();
  const res = g.withUploadedFilesByGenerateContent(fileList).generateContent({ q: "..." });
  console.log(res);
}

JSON Output Control
/**
 * JSON出力制御
 * ・response_mime_type設定: { response_mime_type: "application/json" }
 * ・JSONスキーマ指定: generateContent({ jsonSchema })
 * ・構造化結果取得: JSON形式でパース済みオブジェクト
 */
function myFunction() {
  const apiKey = "###";
  const g = GeminiWithFiles.geminiWithFiles({ apiKey, response_mime_type: "application/json" });
  const jsonSchema = { title: "...", type: "array", items: { type: "object", properties: { recipe_name: { type: "string" } } } };
  const res = g.generateContent({ jsonSchema });
  console.log(res);
}

Sample Code

Post a Sample

No sample codes for this library yet

Be the first to post a sample!

Post a Sample