| 66 | } |
| 67 | |
| 68 | function formatReport() { |
| 69 | auth(); |
| 70 | project = 'bigquerycourse-onetwothree'; |
| 71 | var ss = SpreadsheetApp.getActiveSpreadsheet(); |
| 72 | var aggSheet = ss.getSheetByName('Comments'); |
| 73 | var reportSheet = ss.getSheetByName('Report') || ss.insertSheet('Report'); |
| 74 | reportSheet.clear(); |
| 75 | |
| 76 | var aggData = aggSheet.getDataRange().getValues(); |
| 77 | |
| 78 | cache = CacheService.getUserCache(); |
| 79 | token = cache.get("token"); |
| 80 | if (token == "") return "ERROR"; |
| 81 | Logger.log(`Token = ${token}`); |
| 82 | url = `https://us-central1-aiplatform.googleapis.com/v1/projects/${project}/locations/us-central1/publishers/google/models/gemini-1.5-pro:generateContent` |
| 83 | data = { |
| 84 | contents: { |
| 85 | role: "USER", |
| 86 | parts: { "text": "I will provide you with YouTube Comments assess the sentiment in 5 sections. 1. Overall Sentiment of the video with positive, negative and neutral proportions stated along with 100 word overall summary. 2: Postive Sentiment - Summarise the positive sentiment and provide three reference tweets. Following the same pattern for Negative and Neutral in 3 and 4. 5: Suggestions for video improvement - Provide three key point to improve the next video we make based on the comments." + ' Do not include the video title. Do not create seperate sections for the reference tweets. Only reffer to the data provided and only include title text in title fonts . Full Data:' + aggData} |
| 87 | }, |
| 88 | generation_config: { |
| 89 | temperature: 1, |
| 90 | topP: 1, |
| 91 | maxOutputTokens: 1000 |
| 92 | } |
| 93 | } |
| 94 | const options = { |
| 95 | method: "post", |
| 96 | contentType: 'application/json', |
| 97 | headers: { |
| 98 | Authorization: `Bearer ${token}`, |
| 99 | }, |
| 100 | payload: JSON.stringify(data) |
| 101 | }; |
| 102 | |
| 103 | const response = UrlFetchApp.fetch(url, options); |
| 104 | if (response.getResponseCode() == 200) { |
| 105 | json = JSON.parse(response.getContentText()); |
| 106 | answer = json.candidates[0].content.parts[0].text; |
| 107 | Logger.log(answer); |
| 108 | |
| 109 | // Format the Markdown in Sheets |
| 110 | var lines = answer.split('\n'); |
| 111 | var rowIndex = 1; |
| 112 | |
| 113 | lines.forEach(function(line) { |
| 114 | var cell = reportSheet.getRange(rowIndex, 1); |
| 115 | var text = line.trim(); |
| 116 | |
| 117 | if (text.startsWith('### ')) { |
| 118 | // Handle H3 titles |
| 119 | text = text.replace('### ', ''); |
| 120 | cell.setValue(text) |
| 121 | .setFontWeight('bold') |
| 122 | .setFontSize(14) |
| 123 | .setBackground('#1c4587') // Dark Blue 3 background |
| 124 | .setFontColor('#FFFFFF'); // Set text color to white |
| 125 | } else if (text.startsWith('## ')) { |