Skip to content

Commit d022d81

Browse files
committed
Update sheets apps script code
1 parent 892e8ae commit d022d81

File tree

3 files changed

+28
-67
lines changed

3 files changed

+28
-67
lines changed

extension/src/apps-script/gsheets/Code.gs

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,27 @@
11
function gsheetEvaluate(expression) {
2-
// Use eval to evaluate the string expression
3-
var result = eval(expression);
4-
return JSON.stringify(result);
5-
}
6-
7-
function getUserSelectedRange() {
8-
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
9-
var range = sheet.getActiveRange(); // Get the user's currently selected range
10-
var a1Notation = range.getA1Notation();
11-
return a1Notation;
2+
try {
3+
// Use eval to evaluate the string expression
4+
var result = eval(expression);
5+
Logger.log(result)
6+
// return result;
7+
return JSON.stringify(result);
8+
} catch (e) {
9+
// Handle any errors that occur during evaluation
10+
return "Error: " + e.message;
11+
}
1212
}
1313

14-
function readActiveSpreadsheet(region) {
15-
if (!region) {
16-
region = getUserSelectedRange()
17-
}
18-
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(region);
19-
20-
// Get the values, formulas, and merged status of the range
21-
var values = range.getValues();
22-
var formulas = range.getFormulas();
23-
var numRows = range.getNumRows();
24-
var numColumns = range.getNumColumns();
25-
26-
// Initialize the cells array to store cell data
27-
var cells = [];
14+
function getColumnIndexByValue(sheetName, value) {
15+
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
16+
var range = sheet.getRange(1, 1, 1, sheet.getLastColumn());
17+
var values = range.getValues()[0];
2818

29-
for (var row = 0; row < numRows; row++) {
30-
var rowData = [];
31-
for (var col = 0; col < numColumns; col++) {
32-
var cell = range.getCell(row + 1, col + 1); // Get each cell individually
33-
var cellValue = values[row][col];
34-
var cellType = typeof cellValue;
35-
var isMerged = cell.isPartOfMerge(); // Check if the cell is part of a merged region
36-
var cellData = {
37-
"value": cellValue, // The value of the cell
38-
"type": cellType,
39-
"formula": formulas[row][col] || null, // The formula (or null if there's none)
40-
"isMerged": isMerged // Whether the cell is part of a merged range
41-
};
42-
rowData.push(cellData); // Add the cell data to the row
19+
for (var i = 0; i < values.length; i++) {
20+
if (values[i] == value) {
21+
return i + 1; // Return the column index (1-based)
4322
}
44-
cells.push(rowData); // Add the row data to the cells array
4523
}
46-
47-
// Construct the final output object
48-
var output = {
49-
"region": region, // The range of the spreadsheet being read
50-
"cells": cells // The cells data as an array of arrays
51-
};
52-
53-
Logger.log(JSON.stringify(output)); // Log the result for debugging
54-
55-
return output; // Return the final output object
24+
return -1; // Value not found
5625
}
5726

5827
function onOpen() {

extension/src/apps-script/gsheets/appsscript.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"exceptionLogging": "STACKDRIVER",
55
"runtimeVersion": "V8",
66
"oauthScopes": [
7-
"https://www.googleapis.com/auth/spreadsheets"
7+
"https://www.googleapis.com/auth/spreadsheets",
8+
"https://www.googleapis.com/auth/script.container.ui"
89
],
910
"addOns": {
1011
"sheets": {

extension/src/apps-script/gsheets/index.html

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<!DOCTYPE html>
2-
<html style="width: 850px; height: 100%; background-color: transparent; float:right;">
2+
<html style="width: 300px; height: 100%; background-color: transparent; float:right;">
33
<head>
44
<script>
55
document.addEventListener('DOMContentLoaded', () => {
6-
const TRUSTED_ORIGINS = ["http://localhost:3005"]
7-
// const TRUSTED_ORIGINS = ["https://web.minusxapi.com"]
6+
const TRUSTED_ORIGINS = ["https://web.minusxapi.com"]
7+
// const TRUSTED_ORIGINS = ["http://localhost:3005"]
88
const rpc = {
99
"gsheetEvaluate": () => 1,
1010
}
@@ -14,7 +14,6 @@
1414
return false;
1515
}
1616
function onSuccess(response) {
17-
// console.log('Success!, repsonse is', response)
1817
// document.querySelector("#log").innerHTML = JSON.stringify(response)
1918
event.source.postMessage({
2019
type: 'success',
@@ -25,18 +24,9 @@
2524
});
2625
}
2726
function onFailure(err) {
28-
let errorMessage = ''
29-
try {
30-
errorMessage = err.toString()
31-
} catch (e) {
32-
errorMessage = 'An error occured'
33-
}
34-
// console.log('Failure!, error is', errorMessage)
3527
event.source.postMessage({
3628
type: 'error',
37-
error: {
38-
message: errorMessage
39-
},
29+
error: 'An error occured',
4030
id: payload.id
4131
}, {
4232
targetOrigin: event.origin
@@ -66,7 +56,8 @@
6656
<!-- <div style="display:flex; justify-content: center; align-items: center; flex-direction: column;">
6757
<div id="log" style="width: 300px; display:flex; justify-content: center; align-items: center; flex-direction: column;">Empty</div>
6858
</div> -->
69-
<iframe id="myFrame" src="http://localhost:3005/?tool=google&toolVersion=sheets&width=300&origin=https://docs.google.com" width="100%" height="100%" />
70-
<!-- <iframe id="myFrame" src="https://web.minusxapi.com/?tool=google&toolVersion=sheets&width=300&origin=https://docs.google.com" width="100%" height="100%" /> -->
59+
<!-- <iframe id="myFrame" src="http://localhost:3005/?tool=google&toolVersion=sheets&width=300&origin=https://docs.google.com" width="100%" height="100%" /> -->
60+
<iframe id="myFrame" src="https://web.minusxapi.com/?tool=google&toolVersion=sheets&width=300&origin=https://docs.google.com" width="100%" height="100%" />
7161
</body>
72-
</html>
62+
</html>
63+

0 commit comments

Comments
 (0)