自定义功能有问题

时间:2019-07-08 17:04:28

标签: google-apps-script google-sheets custom-function

我正在将一些电子表格从Excel转换为Google表格,并且我正在尝试执行一个功能,该功能可以获取代码并应用该功能。例如,我在列中有一个代码列表,在接下来的5列中有文本。我想输入 // if your method has return data you could set castToType to // convert the return data to your desire output await PostAsync(solrCoreConnection + "/update",new {commit= true, Id=5}); ,我想用这个公式=myFunction("code")返回值,这将返回第3列以及代码所在的行。

我已经尝试过这些:

=vlookup(code;A1:F30;3;0)

它说我没有调用setFunction的权限,

function myFunc(code) {  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var cell = sheet.getRange(getCell);
  var test = cell.setFormula('=VLOOKUP('+code+';a1:b10;2;0)')
  return test;
}

它打印出我需要的确切代码,但不像公式那样。

1 个答案:

答案 0 :(得分:0)

由于我们不知道getCell的值,因此无法评估您的代码。

在“ lookup”下面的代码中,是一个命名范围。范围是单元格E4,其中包含值“ ghi”,即搜索项。

function test01(){

 var abc = "lookup";  
  so5693959103(abc);

}
function so5693959103(abc) {  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "56939591";
  var sheet = ss.getSheetByName(sheetname);

  // Lookup value is the named range = lookup which is found in E4
  var vlookupformula03 = "=VLOOKUP("+abc+",A1:B10,2,0)";

  //insert formula into cell E8
  var targetRange03 = sheet.getRange("E10");

  targetRange03.setFormula(vlookupformula03);
  Logger.log("DEBUG: result03 = "+targetRange03.getValue());

}

Vlookup屏幕截图
Screenshot


修改

此修订版适用于功能值=mylookup("xxx"),其中'xxx'是查找值。 在下面的示例中,查找值为“ ghi”


/*
Imitates the Vlookup function. Receives:
search - The desired value to look for in the column.
Once the cell of the [search] has been found, the returned parameter would be the value of the cell which is 1 cell to the right of the found cell.
*/
function mylookup(search) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "56939591";
  var sheet = ss.getSheetByName(sheetname);
  var thevalue = search;
  var lastRow=sheet.getLastRow();
  var data=sheet.getRange(1,1,lastRow,2).getValues();

  for(i=0;i<data.length;++i){
    if (data[i][0]==thevalue){
      return data[i][1];
    }
  }
}

我的查询功能

mylooup screenshot