我想要的功能是:
Click on a menu option
Interactive box comes up with 6 textboxes that correspond with a label each
In two of those boxes, at most three, a number is inputted
Corresponding to an algorithm those numbers will change the values in the current active cell range if the value in the cell (a letter) is equal to the label of any of the earlier textbox inputted values, the empty cells get left empty, others that have no correspondence will be changed to 0.
Click OK, the active selected cell range changes accordingly.
我希望将此功能嵌入到电子表格中。我已经开始了,但由于我没有对GAS做过任何事情,我偶然发现了一些问题,需要一个知道如何使用GAS的人的解决方案。我不知道这个功能是否可能像我设想的那样。
function calculateScores() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveRange();
var values = range.getValues();
for (var i = 0; i <= values.length - 1; i++) {
var row = values[i];
Logger.log(row);
}
};
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Calculate Scores",
functionName : "calculateScores"
}];
spreadsheet.addMenu("Calculate Scores", entries);
};
这将创建菜单项并仅记录所选范围。接下来,如何使用其余的界面和功能来解决问题?
答案 0 :(得分:4)
您可以使用UiApp执行此操作。我在下面放了一些示例代码,可能会帮助您。
您可以找到有关UiApp in the documentation here的有用信息。
Waqar Ahmed's site也有很多我发现非常有帮助的例子。
function calculateScores() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Create the user interface
var app = UiApp.createApplication();
//Create two textboxes
var tBox1 = app.createTextBox().setName('tBox1').setId('tBox1');
var tBox2 = app.createTextBox().setName('tBox2').setId('tBox2');
//Create the OK button and a handler to respond to when OK is clicked.
var okHandler = app.createServerHandler('respondToOK');
var btnOK = app.createButton('OK', okHandler);
//I find grids very handy for arranging labels and widgets like textboxes neatly
var myGrid = app.createGrid(3, 2).setId('myGrid').setStyleAttribute('background', 'lightCyan')
.setText(0, 0, 'Label for text box 1')
.setWidget(0, 1, tBox1)
.setText(1, 0, 'Label for text box 2')
.setWidget(1, 1, tBox2)
.setWidget(2, 1, btnOK);
/*The OK button's handler needs a callback element otherwise it can't read the user's input.
You could add multiple callback elements to each of the textboxes but that's just extra code.
As all the textboxes are added to myGrid then just one callback to that will work fine.
*/
okHandler.addCallbackElement(myGrid);
app.add(myGrid);
ss.show(app);
}
function respondToOK(e) {
var app = UiApp.getActiveApplication();
var entry1 = e.parameter.tBox1;
var entry2 = e.parameter.tBox2;
//Substitute your own algorhythm here, I just multiplied the two entries
var result = entry1 * entry2;
//Enter the result in the active cell
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cellAddress = ss.getActiveCell().getA1Notation();
ss.getActiveSheet().getRange(cellAddress).setValue(result);
//Close the user interface
return app.close();
}
希望这会有所帮助。祝你好运!