Google apps script 停止自定义函数自动刷新/定期调用外部API
我正在使用GoogleApps脚本和自定义函数调用外部API来验证电话号码 下面是我函数的代码Google apps script 停止自定义函数自动刷新/定期调用外部API,google-apps-script,custom-function,Google Apps Script,Custom Function,我正在使用GoogleApps脚本和自定义函数调用外部API来验证电话号码 下面是我函数的代码 /** * This CUSTOM FUNCTION uses the numVerify API to validate * a phone number based on the input from JotForm and a * country code which is derived from the JotForm country * * Numverify website
/**
* This CUSTOM FUNCTION uses the numVerify API to validate
* a phone number based on the input from JotForm and a
* country code which is derived from the JotForm country
*
* Numverify website: https://numverify.com/dashboard (account via LastPass)
* Numverify docs: https://numverify.com/documentation
*/
function PHONE_CHECK(number, country){
if(country == "")
return [["", "country_not_set"]]
// check the API result has already been retrieved
var range = SpreadsheetApp.getActiveSheet().getActiveRange()
var apires = range.offset(0, 1).getValue()
if(apires.length > 0)
return range.offset(0, 0, 1, 2).getValues()
var url = 'http://apilayer.net/api/validate'
+ '?access_key=' + NUMVERIFY_KEY
+ '&number=' + encodeURIComponent(number)
+ '&country_code=' + encodeURIComponent(country)
+ '&format=1';
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
var data = JSON.parse(json);
if(data.valid !== undefined){
if(data.valid){
return [[data.international_format, "OK"]]
}else{
return [["", "invalid_number"]] // overflows data to the next column (API Error) while keeping the phone field clear for import into TL
}
}else if(data.success !== undefined){
if(data.error.type.length > 0){
return [[number, data.error.type]]
}else{
return [[number, "no_error_type"]]
}
}else{
return [[number, "unexpected_error"]] // this generally shouldn't happen...
}
}
根据这个公式,它需要一个电话号码和国家代码,然后它会根据numverify API检查电话号码,并将结果返回到单元格中,然后溢出到单元格右侧的单元格中。溢出用于指示API是否已成功调用,并检查结果是否已检索到
例如:
=电话检查(“+32123456789”,“BE”)
请注意,第一个单元格为空,因为API返回“无效电话号码”代码。出于隐私考虑,我不会在这里放任何真实的电话号码。如果我使用的是真实的电话号码,第一个手机将包含国际号码格式的电话号码
由于我使用的是免费计划,如果我已经知道结果是什么,我不想每次都重新运行该函数,因为我不想遇到费率限制不幸的是,这似乎不起作用并且会定期(看起来每天一次)刷新工作表中每一行的结果
所以有两个问题:
函数arrayTest(){
返回[[1,2,3,4,5]]
}
如果将其放在单元格中,如下所示:
您将看到,如果删除原始单元格中的公式,溢出值也会消失
因此,类似以下代码的内容几乎总是会产生相同的值:
功能检查测试(){
var cell=SpreadsheetApp.getActiveRange()
var status=cell.offset(0,1).getValue();
如果(状态!=“”){
在不调用API的情况下返回“已执行”//
}否则{
return[[“OK”,“executed”]]//在您的情况下,进行API调用-将发生约90%的时间。
}
}
//输出[[“确定”,“已执行”]]
这里我插入一行并删除它,以强制重新计算公式
在重新计算公式之前,工作表要做的第一件事是清除以前由公式填充的值。由于条件语句取决于其先前执行的值,因此它的计算结果总是相同的。在您的情况下,它几乎总是进行API调用
令人困惑的是,这不是100%可靠!你会发现有时候,它会按照你的意愿工作。虽然在我的测试中,这种情况只发生了大约10次中的1次,而且最常见的情况是在将更改保存到脚本编辑器时更新公式
理想情况下,尽管不可能,但您希望能够编写如下内容:
功能检查测试(){
var cell=SpreadsheetApp.getActiveRange();
var cellValue=cell.getValue();
var adjacentCell=单元偏移量(0,1);
var status=adjacentCell.getValue();
如果(状态==“”){
cell.setValue(cellValue)
setValue(“已执行”)
}
}
这将在公式运行后清除公式,唉,setValue()
对公式禁用了!如果要使用setValue()
,则需要从菜单、触发器或脚本编辑器运行脚本。在这种情况下,它不再是一个有意义的公式
工具书类
你的目标:
您希望自定义函数(即公式)只运行一次,或运行生成特定结果所需的次数。
您希望相同的公式向另一个单元格(例如相邻单元格)写入一个值,该值将在将来告诉公式是否应再次运行
简短答复:
我担心,从自定义函数或公式计算的值是暂时的,您想要用它们实现的是不可能的
说明:
您可以使用此自定义函数运行快速测试:
函数arrayTest(){
返回[[1,2,3,4,5]]
}
如果将其放在单元格中,如下所示:
您将看到,如果删除原始单元格中的公式,溢出值也会消失
因此,类似以下代码的内容几乎总是会产生相同的值:
功能检查测试(){
var cell=SpreadsheetApp.getActiveRange()
var status=cell.offset(0,1).getValue();
如果(状态!=“”){
在不调用API的情况下返回“已执行”//
}否则{
return[[“OK”,“executed”]]//在您的情况下,进行API调用-将发生约90%的时间。
}
}
//输出[[“确定”,“已执行”]]
这里我插入一行并删除它,以强制重新计算公式
在重新计算公式之前,工作表要做的第一件事是清除以前由公式填充的值。由于条件语句取决于其先前执行的值,因此它的计算结果总是相同的。在您的情况下,它几乎总是进行API调用
令人困惑的是,这不是100%可靠!你会发现有时候,它会按照你的意愿工作。图格
var range = SpreadsheetApp.getActiveSheet().getActiveRange() // get the cell from which the function is called
var apires = range.offset(0, 1).getValue() // get the values directly to the right of the cell
if(apires.length > 0) // check if there's anything there...
return range.offset(0, 0, 1, 2).getValues() // return an array that basically just resets the same values, effectively stopping the script from running