【发布时间】:2019-07-04 19:34:04
【问题描述】:
我有一个自定义函数,可以找到另一个单元格的值并显示它。当源单元格改变时,函数不反映。
https://docs.google.com/spreadsheets/d/1wfFe__g0VdXGAAaPthuhmWQo3A2nQtSVUhfGBt6aIQ0/edit?usp=sharing
刷新谷歌表格
function findRate() {
var accountName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(1,1).getValue(); //determine the account name to use in the horizontal search
var rateTab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Rates'); //hold the name of the rate tab for further dissection
var rateNumColumns =rateTab.getLastColumn(); //count the number of columns on the rate tab so we can later create an array
var rateNumRows = rateTab.getLastRow(); //count the number of rows on the rate tab so we can create an array
var rateSheet = rateTab.getRange(1,1,rateNumRows,rateNumColumns).getValues(); //create an array based on the number of rows & columns on the rate tab
var currentRow = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getRow(); //gets the current row so we can get the name of the rate to search
var rateToSearch = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(currentRow,1).getValue(); //gets the name of the rate to search on the rates tab
for(rr=0;rr<rateSheet.length;++rr){
if (rateSheet[rr][0]==rateToSearch){break} ;// if we find the name of the
}
for(cc=0;cc<rateNumColumns;++cc){
if (rateSheet[0][cc]==accountName){break};
}
var rate = rateSheet[rr][cc] ; //the value of the rate as specified by rate name and account name
return rate;
}
如果我在汇率选项卡中更改汇率,我需要自定义函数来识别新汇率并更新其值
【问题讨论】:
-
自定义函数被确定性地评估。根据谷歌的说法,如果一个人不接受任何参数,那么它就不需要重新计算。解决方案?传递论据!您显然依赖电子表格中的单元格进行计算,因此请明确说明,例如
C4: =FINDRATE(A4)。那么当A4改变时,C4中的这个公式也会改变。
标签: google-apps-script google-sheets custom-function