ChatGPT v Excelu – jak poslat otázku a dostat odpověď
S tímto tématem se setkáte na našich kurzech ExcelTown.
Aktuálně: kurzy můžete absolvovat jak online, tak prezenčně.
Autor: Alžběta Průchová
Tento návod popisuje, jak v Excelu odeslat otázku do ChatGPT a dostat odpověď.
Pro použití stačí stáhnout si soubor:
vložit svůj klíč z ChatGPT a pak použít tento Office Script:
// Office script for ChatGPT
async function main(workbook: ExcelScript.Workbook) {
const apiKey = workbook.getWorksheet("API").getRange("B1").getValue();
const endpoint: string = "https://api.openai.com/v1/completions";
// Info o listu, kam píšu dotaz
const sheet = workbook.getWorksheet("Dotaz");
// napiš dotaz
const mytext = sheet.getRange("B2").getValue();
// Pokud GPT vrací hodnotu, co má více řádků, zapíše do pomocného listu
const result = workbook.getWorksheet("Vysledek");
result.getRange("A1:D1000").clear();
sheet.getRange("B3").setValue(" ")
// Nastavení engine modelu
const model: string = "text-davinci-002";
const prompt: (string | boolean | number) = mytext;
// Nastavení HTTP záhlaví
const headers: Headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", `Bearer ${apiKey}`);
// Nastavení HTTP body
const body: (string | boolean | number) = JSON.stringify({
model: model,
prompt: prompt,
max_tokens: 1024,
n: 1,
temperature: 0.5,
});
// Odeslání HTTP požadavku
const response: Response = await fetch(endpoint, {
method: "POST",
headers: headers,
body: body,
});
// Parsování JSON
const json: { choices: { text: (string | boolean | number) }[] } = await response.json();
// Get the answer - i.e. output
const text: (string | boolean | number) = json.choices[0].text;
// Výstup vygenerovaného textu
// console.log(text);
const output = sheet.getRange("B4");
output.setValue(text);
const cell = sheet.getRange("B4");
// Rozdělení obsahu buňky po řádcích
const arr = cell.getValue().toString().split("\n");
const newcell = result.getRange("A1");
var offset = 0;
// console.log (arr)
for (let i = 0; i < arr.length; i++) {
// Napiš hodnotu do další buňky
if (arr[i].length > 0) {
newcell.getOffsetRange(offset, 0).setValue(arr[i]);
offset++;
}
}
// console.log(offset)
if (offset > 1) {
sheet.getRange("B3").setValue("Zkontroluj list 'Vysledek'")
}
}
S tímto tématem se setkáte na našich kurzech ExcelTown.
Aktuálně: kurzy můžete absolvovat jak online, tak prezenčně.