Last active
July 15, 2024 18:50
-
-
Save 2RiniaR/ad9c239d612f0ab99d683f6762fff172 to your computer and use it in GitHub Desktop.
Gemini AIが自動でコメントしてくれる日記のGAS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
const GEMINI_API = PropertiesService.getScriptProperties().getProperty("GEMINI_API_KEY"); | |
const ERROR_COMMENT = "コメントが取得できませんでした。"; | |
function main() { | |
const now = new Date(); | |
const targetDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1); | |
Logger.log(`対象: ${targetDate}`); | |
// 今月のシートを取得 | |
const sheetName = Utilities.formatDate(targetDate, "JST", "yyyy/MM"); | |
Logger.log(`シート読み込み: ${sheetName}`); | |
const sheet = spreadsheet.getSheetByName(sheetName); | |
if (sheet === null) { | |
Logger.log("エラー:シートが存在しません。"); | |
return; | |
} | |
// 日付が今日になっている列を取得 | |
const cellName = Utilities.formatDate(targetDate, "JST", "M/d ({曜日})").replace("{曜日}", getDayFormat(targetDate)); | |
Logger.log(`日付検索: ${cellName}`); | |
const dateCell = sheet | |
.getRange(1, 1, 1, sheet.getLastColumn()) | |
.createTextFinder(cellName) | |
.findNext(); | |
if (dateCell === null) { | |
Logger.log("エラー:セルが存在しません。"); | |
return; | |
} | |
const column = dateCell.getColumn(); | |
Logger.log(`本文取得: (2, ${column})`); | |
const content = sheet.getRange(2, column).getValue(); | |
if (content === null || content === "") { | |
Logger.log("エラー:今日の本文がありません。"); | |
return; | |
} | |
Logger.log(`Geminiからコメント取得`); | |
const comment = generateCommentFromAI(content); | |
Logger.log(`コメント記入: (3, ${column})`); | |
sheet.getRange(3, column).setValue(comment ?? ERROR_COMMENT); | |
Logger.log("完了"); | |
} | |
function generateCommentFromAI(content) { | |
const queryLines = [ | |
"SNSに投稿された以下の日記に対して、彼のファンとしてコメントを4つ挙げてください。ただし、各コメントは140字以内で、箇条書きの形式でお願いします。回答以外の内容は含めないでください。", | |
"", | |
"```", | |
content, | |
"```" | |
]; | |
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${GEMINI_API}` | |
const payload = { | |
'contents': [ | |
{ | |
'parts': [{ | |
'text': queryLines.join("\n") | |
}] | |
} | |
] | |
} | |
const options = { | |
'method': 'post', | |
'contentType': 'application/json', | |
'payload': JSON.stringify(payload) | |
}; | |
const res = UrlFetchApp.fetch(url, options) | |
const resJson = JSON.parse(res.getContentText()); | |
if (resJson && resJson.candidates && resJson.candidates.length > 0) { | |
return resJson.candidates[0].content.parts[0].text; | |
} else { | |
return null; | |
} | |
} | |
function getDayFormat(date) { | |
switch(date.getDay()){ | |
case 0: return '日'; | |
case 1: return '月'; | |
case 2: return '火'; | |
case 3: return '水'; | |
case 4: return '木'; | |
case 5: return '金'; | |
case 6: return '土'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
トリガー設定こんな感じ

毎朝5時に
main()
を実行する