2020년 8월 16일 일요일

Balancing with Google Apps Script

I learned Google Apps Script for fun. My current task is balancing game. I use google spread sheet for balancing game. There are some calculation that can't be done only with functions in Google Spread Sheet. At that moment, Google Apps Script is very useful. 

1. Use function with button

 I wrote calculation that use random function. I wanted to refresh the result anytime I want to. But to click refresh button is not good. I found easy way. This way is using a Google Apps Script.  

 1) You put a button in spreadsheet. 




 2) Right click the button you made. Then choose 'allocate script'. 

3) Allocate the function you write at a script editor. When doing this, just write the name of function. No '()' allowed.  
4) You can write functions in Google Apps Script here. 
Upper menu bar> Tool> Script Editor



Ignore other code from 'act' function. All I wanted is 'flush' method. 'flush' method refresh every value that are linked to 'current time' or 'random' function. 

(It shows 'K16' over there.  When you write that in Spreadsheet, not in script editor, you must write "K16" not 'K16')


Yet I am learning Google Apps Script (painfully) slowly. 

There is no return type as 'cell' instead there is 'range' return type. When you need to return some value into cell, use 'range'. 

You can learn easily with this tutorial. 


2. Use customized function in the cell with writing the name of the function

Let's try with easy one. 
When you put a value from other cell. Your function will be like this. 

function getChart(x) {
  var sheet  = SpreadsheetApp.getActive().getRange(x).getValue();
  return sheet;
}

And you use that function in your Spreadsheet like this. 

            SpreadsheetApp.getActive()      :Select the spreadsheet that is open now. 
            getRange()                                  :Select the cells your want to use. You can input single cell or 
                                                                 some range.
            getValue()                                   :get value from the selected cells. 

When you write function in the Spreadsheet, don't ";"(semi colon).  










댓글 없음:

댓글 쓰기