ChatGPT 4では機能が強化されましたが、トークンのコストが安いため、ChatGPT 3.5を好むユーザーもいます。しかし、ChatGPT 3.5にはファイルアップロード機能がないため、ユーザーはデータシートを分析することができません。この記事では、この制限を克服し、OpenAI APIを使ってスプレッドシートを分析できるONLYOFFICEマクロの作成方法をご紹介します。
マクロについて
この制限を回避するために、私たちのマクロは以下のステップを踏む:
- スプレッドシートから選択したセルの値を収集する。
- これらの値を配列にまとめる。
- この配列を文字列に変換する。
- Node.jsプロキシサーバーにfetchリクエストで送信する。
- サーバーはリクエストボディから配列を取得する。
- そして、OpenAIライブラリを利用して、OpenAIにAPIリクエストを送信する。
- レスポンスを受信すると、サーバはレスポンスオブジェクトをマクロに返す。
完全なコードを含むプロキシサーバのセットアップの詳細な手順については、OpenAI からのデータをスプレッドシートに入力するためのマクロの作成方法を紹介するブログ投稿をご覧ください。 ご注意:このマクロは、ChatGPT 3.5モデルのトークンが4096トークンという制限があるため、通常50行程度の中規模のテーブルに最適であることに注意してください。
マクロの構築
まず、スプレッドシートで現在選択されている範囲を取得します:
// Get the selected range using ONLYOFFICE API var selection = Api.GetSelection();
次にrowDataという空の配列を作り、選択したセルから収集するデータを格納します:
// Initialize an array to store all data var rowData = [];
ForEachループを使って、選択範囲の各セルを繰り返し処理します。各セルについて、GetValue メソッドで値を取得し、その値を rowData 配列に追加します:
// Use ForEach to iterate through the selected range selection.ForEach(function (cell) { // Retrieve the cell value using ONLYOFFICE API var cellValue = cell.GetValue(); // Add cell value to the rowData array rowData.push(cellValue); });
その後、rowData配列に集められた値を、カンマで区切られた1つの文字列に変換します:
// Merge the values in rowData and separate them by commas var rowDataAsString = rowData.join(',');
requestDataというオブジェクトを作成します:
// Prepare the data to send in the POST request var requestData = { prompt: `analyze this data ${rowDataAsString}`, // Use the merged string here apiKey: '<APIkey>', // Replace with your API key };
- prompt フィールドには、分析用のセル値のマージされた文字列が含まれる。
- apiKey フィールドには、Node.js サーバーが受信フェッチ・リクエストを認証するために使用する API キーが含まれる。
次に fetch 関数を使用して、指定された URL に POST リクエストを送信します:
fetch('http://localhost:3000/completion', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(requestData), })
APIのレスポンスを処理するために、まずJSON形式に変換し、コンソールにデータをロギングし、POSTリクエストに問題があった場合のエラー処理も実装します:
.then(function (response) { return response.json(); }) .then(function (data) { // Log the API response console.log(data); }) .catch(function (error) { // Handle any errors that occur during the fetch console.error('Error:', error); });
マクロコード全体は以下の通りになります:
(function() { // Get the selected range using ONLYOFFICE API var selection = Api.GetSelection(); // Initialize an array to store all data var rowData = []; // Use ForEach to iterate through the selected range selection.ForEach(function (cell) { // Retrieve the cell value using ONLYOFFICE API var cellValue = cell.GetValue(); // Add cell value to the rowData array rowData.push(cellValue); }); // Merge the values in rowData and separate them by commas var rowDataAsString = rowData.join(','); // Prepare the data to send in the POST request var requestData = { prompt: `analyze this data ${rowDataAsString}`, // Use the merged string here apiKey: '<APIkey>', // Replace with your API key }; // Send the data to the API (replace the URL with your OpenAI API endpoint) fetch('http://localhost:3000/completion', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(requestData), }) .then(function (response) { return response.json(); }) .then(function (data) { // Log the API response console.log(data); }) .catch(function (error) { // Handle any errors that occur during the fetch console.error('Error:', error); }); })();
このブログ記事で共有された洞察が、仕事の効率化に貢献できることを願っています。ぜひ、私たちの様々なAPI手法を探求し、日々の業務に導入してください。 お問い合わせや独創的なアイデアがありましたら、遠慮なくお聞かせください。私たちはオープンで、コラボレーションの可能性に熱心です。