|
1 | 1 | 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 | + } |
12 | 12 | } |
13 | 13 |
|
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]; |
28 | 18 |
|
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) |
43 | 22 | } |
44 | | - cells.push(rowData); // Add the row data to the cells array |
45 | 23 | } |
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 |
56 | 25 | } |
57 | 26 |
|
58 | 27 | function onOpen() { |
|
0 commit comments