MCPcopy Create free account
hub / github.com/analyticswithadam/App_Script / formatReport

Function formatReport

Big Query and Gemini Narrative Reporting.js:50–174  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

48}
49
50function formatReport() {
51 auth();
52 var ss = SpreadsheetApp.getActiveSpreadsheet();
53 var rawSheet = ss.getSheetByName('RawData');
54 var reportSheet = ss.getSheetByName('Report') || ss.insertSheet('Report');
55 reportSheet.clear();
56
57 var rawData = rawSheet.getDataRange().getValues();
58
59 cache = CacheService.getUserCache();
60 token = cache.get("token");
61 if (token == "") return "ERROR";
62 Logger.log(`Token = ${token}`);
63 url = `https://us-central1-aiplatform.googleapis.com/v1/projects/${project}/locations/us-central1/publishers/google/models/gemini-1.5-pro:generateContent`
64 data = {
65 contents: {
66 role: "USER",
67 parts: { "text": "You are a public policy expert. I providing you with three years of 311 service request data from San Francisco. Write a report commenting on improvements and negative movements from the period in question, include the absolute and percentage movement of categories in the discussion, do not include a table. Finish with a five point plan of where we need to put resources. Focus only on the data provided and double check calculations" + rawData}
68 },
69 generation_config: {
70 temperature: 0.3,
71 topP: 1,
72 maxOutputTokens: 1000
73 }
74 }
75 const options = {
76 method: "post",
77 contentType: 'application/json',
78 headers: {
79 Authorization: `Bearer ${token}`,
80 },
81 payload: JSON.stringify(data)
82 };
83
84 const response = UrlFetchApp.fetch(url, options);
85 if (response.getResponseCode() == 200) {
86 json = JSON.parse(response.getContentText());
87 answer = json.candidates[0].content.parts[0].text;
88 Logger.log(answer);
89
90// Format the Markdown in Sheets
91var lines = answer.split('\n');
92 var rowIndex = 1;
93
94 lines.forEach(function(line) {
95 var cell = reportSheet.getRange(rowIndex, 1);
96 var text = line.trim();
97
98 if (text.startsWith('### ')) {
99 // Handle H3 titles
100 text = text.replace('### ', '');
101 cell.setValue(text)
102 .setFontWeight('bold')
103 .setFontSize(14)
104 .setBackground('#efefef');
105 } else if (text.startsWith('## ')) {
106 // Handle H2 titles
107 text = text.replace('## ', '');

Callers 1

generateReportFunction · 0.70

Calls 1

authFunction · 0.70

Tested by

no test coverage detected