MCPcopy Index your code
hub / github.com/analyticswithadam/App_Script / formatReport

Function formatReport

Data to Narrative with Input.js:93–215  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

91}
92
93function formatReport() {
94 auth();
95 var input = pullVariables()
96 var ss = SpreadsheetApp.getActiveSpreadsheet();
97 var rawSheet = ss.getSheetByName('RawData');
98 var aggSheet = ss.getSheetByName('Pivot');
99 var reportSheet = ss.getSheetByName('Report') || ss.insertSheet('Report');
100 reportSheet.clear();
101
102 var rawData = rawSheet.getDataRange().getValues(); // Unpivoted Data - Not Using below
103 var aggData = aggSheet.getDataRange().getValues(); // Pivot Data
104
105 cache = CacheService.getUserCache();
106 token = cache.get("token");
107 if (token == "") return "ERROR";
108 Logger.log(`Token = ${token}`);
109 url = `https://us-central1-aiplatform.googleapis.com/v1/projects/${project}/locations/us-central1/publishers/google/models/gemini-1.5-flash:generateContent`
110 data = {
111 contents: {
112 role: "USER",
113 parts: { "text": `${input.var5}` + ' Only reffer to the data provided do not make anything up. Full Data:' + aggData}
114 },
115 generation_config: {
116 temperature: 0.3,
117 topP: 1,
118 maxOutputTokens: 1000
119 }
120 }
121 const options = {
122 method: "post",
123 contentType: 'application/json',
124 headers: {
125 Authorization: `Bearer ${token}`,
126 },
127 payload: JSON.stringify(data)
128 };
129
130 const response = UrlFetchApp.fetch(url, options);
131 if (response.getResponseCode() == 200) {
132 json = JSON.parse(response.getContentText());
133 answer = json.candidates[0].content.parts[0].text;
134 Logger.log(answer);
135
136// Format the Markdown in Sheets
137var lines = answer.split('\n');
138 var rowIndex = 1;
139
140 lines.forEach(function(line) {
141 var cell = reportSheet.getRange(rowIndex, 1);
142 var text = line.trim();
143
144 if (text.startsWith('### ')) {
145 // Handle H3 titles
146 text = text.replace('### ', '');
147 cell.setValue(text)
148 .setFontWeight('bold')
149 .setFontSize(14)
150 .setBackground('#083763') // Dark Blue 3 background

Callers 1

generateReportFunction · 0.70

Calls 2

authFunction · 0.70
pullVariablesFunction · 0.70

Tested by

no test coverage detected