|
| 1 | +/* |
| 2 | +Copyright 2024 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/** |
| 18 | + * Creates a menu entry in the Google Sheets Extensions menu when the document is opened. |
| 19 | + * |
| 20 | + * @param {object} e The event parameter for a simple onOpen trigger. |
| 21 | + */ |
| 22 | +function onOpen(e) { |
| 23 | + SpreadsheetApp.getUi().createAddonMenu() |
| 24 | + .addItem('📄 Open AutoSummarize AI', 'showSidebar') |
| 25 | + .addSeparator() |
| 26 | + .addItem('❎ Quick summary', 'doAutoSummarizeAI') |
| 27 | + .addItem('❌ Remove all summaries', 'removeAllSummaries') |
| 28 | + .addToUi(); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Runs when the add-on is installed; calls onOpen() to ensure menu creation and |
| 33 | + * any other initializion work is done immediately. This method is only used by |
| 34 | + * the desktop add-on and is never called by the mobile version. |
| 35 | + * |
| 36 | + * @param {object} e The event parameter for a simple onInstall trigger. |
| 37 | + */ |
| 38 | +function onInstall(e) { |
| 39 | + onOpen(e); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Opens sidebar in Sheets with AutoSummarize AI interface. |
| 44 | + */ |
| 45 | +function showSidebar() { |
| 46 | + const ui = HtmlService.createHtmlOutputFromFile('sidebar') |
| 47 | + .setTitle('AutoSummarize AI'); |
| 48 | + SpreadsheetApp.getUi().showSidebar(ui); |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +/** |
| 53 | + * Deletes all of the AutoSummarize AI created sheets |
| 54 | + * i.e. any sheets with prefix of 'AutoSummarize AI' |
| 55 | + */ |
| 56 | +function removeAllSummaries() { |
| 57 | + const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); |
| 58 | + const allSheets = spreadsheet.getSheets(); |
| 59 | + |
| 60 | + allSheets.forEach(function (sheet) { |
| 61 | + const sheetName = sheet.getName(); |
| 62 | + // Check if the sheet name starts with "AutoSummarize AI" |
| 63 | + if (sheetName.startsWith("AutoSummarize AI")) { |
| 64 | + spreadsheet.deleteSheet(sheet) |
| 65 | + } |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Wrapper function for add-on. |
| 71 | + */ |
| 72 | +function doAutoSummarizeAI(customPrompt1, customPrompt2, temperature = .1, tokens = 2048) { |
| 73 | + // Get selected cell values. |
| 74 | + console.log("Getting selection..."); |
| 75 | + let selection = SpreadsheetApp.getSelection() |
| 76 | + .getActiveRange() |
| 77 | + .getRichTextValues() |
| 78 | + .map(value => { |
| 79 | + if (value[0].getLinkUrl()) { |
| 80 | + return value[0].getLinkUrl(); |
| 81 | + } |
| 82 | + return value[0].getText(); |
| 83 | + }); |
| 84 | + |
| 85 | + // Get AI summary |
| 86 | + const data = summarizeFiles(selection, customPrompt1, customPrompt2, temperature, tokens); |
| 87 | + |
| 88 | + // Add and format a new new sheet. |
| 89 | + const now = new Date(); |
| 90 | + const nowFormatted = Utilities.formatDate(now, now.getTimezoneOffset().toString(), "MM/dd HH:mm"); |
| 91 | + let sheetName = `AutoSummarize AI (${nowFormatted})`; |
| 92 | + if (SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)) { |
| 93 | + sheetName = `AutoSummarize AI (${nowFormatted}:${now.getSeconds()})`; |
| 94 | + } |
| 95 | + let aiSheet = SpreadsheetApp.getActiveSpreadsheet() |
| 96 | + .insertSheet() |
| 97 | + .setName(sheetName); |
| 98 | + let aiSheetHeaderStyle = SpreadsheetApp.newTextStyle() |
| 99 | + .setFontSize(12) |
| 100 | + .setBold(true) |
| 101 | + .setFontFamily("Google Sans") |
| 102 | + .setForegroundColor("#ffffff") |
| 103 | + .build(); |
| 104 | + let aiSheetValuesStyle = SpreadsheetApp.newTextStyle() |
| 105 | + .setFontSize(10) |
| 106 | + .setBold(false) |
| 107 | + .setFontFamily("Google Sans") |
| 108 | + .setForegroundColor("#000000") |
| 109 | + .build(); |
| 110 | + aiSheet.getRange("A1:E1") |
| 111 | + .setBackground("#434343") |
| 112 | + .setTextStyle(aiSheetHeaderStyle) |
| 113 | + .setValues([["Link", "Title",`Summary from Gemini AI [Temperature: ${temperature}]`, `Custom Prompt #1: ${customPrompt1}`, `Custom Prompt #2: ${customPrompt2}`]]) |
| 114 | + .setWrap(true); |
| 115 | + aiSheet.setColumnWidths(1, 1, 100); |
| 116 | + aiSheet.setColumnWidths(2, 1, 300); |
| 117 | + aiSheet.setColumnWidths(3, 3, 300); |
| 118 | + |
| 119 | + // Copy results |
| 120 | + aiSheet |
| 121 | + .getRange(`A2:E${data.length + 1}`) |
| 122 | + .setValues(data); |
| 123 | + |
| 124 | + aiSheet.getRange(`A2:E${data.length + 1}`) |
| 125 | + .setBackground("#ffffff") |
| 126 | + .setTextStyle(aiSheetValuesStyle) |
| 127 | + .setWrapStrategy(SpreadsheetApp.WrapStrategy.CLIP) |
| 128 | + .setVerticalAlignment("top"); |
| 129 | + aiSheet.getRange(`C2:E${data.length + 1}`) |
| 130 | + .setBackground("#efefef") |
| 131 | + .setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP); |
| 132 | + |
| 133 | + aiSheet.deleteColumns(8, 19); |
| 134 | + aiSheet.deleteRows(aiSheet.getLastRow() + 1, aiSheet.getMaxRows() - aiSheet.getLastRow()); |
| 135 | +} |
0 commit comments