将伪参数添加到Google工作表的自定义函数中

时间:2016-07-19 13:10:17

标签: javascript google-apps-script

我有以下自定义功能;它将列从一张纸张移动到Google表格中的另一张纸张

我明白了 Non-contiguous column copy from one spreadsheet to another in google apps

我需要在其中添加dummy参数,以便我可以刷新它所提取的数据

我尝试过这样做:function copyColumns(sourceRange,start,sheetKey, dummy)(已经适用于其他自定义函数)

但我一直收到错误:

Problem getting sheet1 - Exception: You do not have permission to perform that action. (line 31).

这是:throw "Problem getting sheet" + sheetKey + " - " + err;

我知道一些VBA但我是谷歌脚本写作的新手,我已经尝试但没有弄清楚如何做到这一点

由于

/* =copyColumns("MyDataSheet!C,A,W",8) */

function copyColumns(sourceRange,start,sheetKey) {
  // Initialize optional parameter
  if(!sheetKey && typeof start!== "number") {
    sheetKey = start;
    start = 1;
  } else {
    start = start || 1;
  }
  // Check SourceRange Input
  var inputRe = /^((.*?!)(?=[a-z],?|[a-i][a-z]))?[a-i]?[a-z](,[a-i]?[a-z])*$/i;
  if(!inputRe.test(sourceRange))
    throw "Invalid SourceRange: " + sourceRange;

  // Check Start Row
  if(typeof start !== "number")
    throw "Starting row must be a number! Got: " + start;
  if(start % 1 !== 0)
    throw "Starting row must be an integer! Got: " + start;
  if(start < 1)
    throw "Starting row can't be less than 1! Got: " + start;

  // Get the Source Sheet
  try {
    var ss = sheetKey
           ? SpreadsheetApp.openById(sheetKey)
           : SpreadsheetApp.getActiveSpreadsheet();
  }  catch(err) {
    throw "Problem getting sheet" + sheetKey + " - " + err;
  } 
  var sheetName = sourceRange.match(/^.*?(?=!)/);
  var sheet = sheetName
            ? ss.getSheetByName(sheetName[0])
            : ss.getActiveSheet();

  // Check that everything is still valid
  if(!sheet)
    throw "Could not find sheet with name: " + sheetName;
  if(start > sheet.getLastRow())
    throw "No data beyond row: " + start + " Last row: " + sheet.getLastRow();

  // Get the values
  var lastCol = sheet.getLastColumn();
  var lastRow = sheet.getLastRow()-start+1;
  var values = sheet.getRange(start,1,lastRow,lastCol).getValues();

  // Get the desired columns from the string
  var desiredColMatch = sourceRange.match(/([a-i]?[a-z](,[a-i]?[a-z])*)$/i);
  var desiredColumns = desiredColMatch[0].toUpperCase().split(",");

  // In case the column we are trying to grab doesn't exist in the sheet
  var lastColId = sheet.getMaxColumns() - 1; // Array is 0 indexed, Sheet is 1

  // Get the numerical values of the passed in Column Ids
    var columns = desiredColumns.map(function(colId){
    var num = colId.length - 1; // 0 or 1
    var colNum = colId.charCodeAt(num)-65+num*26*(colId.charCodeAt(0)-64);
    if(colNum > lastColId)
      throw "Invalid Column: " + colId + " - Column not in: " + sheetName;
    return colNum;
  });

  //Map the values to a new array of just the columns we want
  return values.map(function(row){
    return columns.map(function(col){
      return row[col]
    })
  });
}

1 个答案:

答案 0 :(得分:1)

该错误与使用伪参数无关。发生此错误是因为自定义函数无法调用需要授权才能运行的服务。具体来说,电子表格服务(SpreadsheetApp)需要运行授权。

来自https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services

  

与大多数其他类型的应用程序脚本不同,自定义功能从不要求用户授权访问个人数据。因此,他们只能拨打无法访问个人数据的服务,......