javascript - 将虚拟参数添加到 Google 表格的自定义函数

标签 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).

即:抛出“获取工作表时出现问题”+sheetKey+“-”+err;

我了解一些 VBA,但对 Google 脚本编写不熟悉,我已经尝试过,但尚未弄清楚如何执行此操作

谢谢

/* =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]
    })
  });
}

最佳答案

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

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

Unlike most other types of Apps Scripts, custom functions never ask users to authorize access to personal data. Consequently, they can only call services that do not have access to personal data, ...

关于javascript - 将虚拟参数添加到 Google 表格的自定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459620/

相关文章:

javascript - 初始化 CSS 链接装饰

google-apps-script - 使用Google应用程序脚本开发Gmail插件时,如何将数据安全地存储在Google中?

google-apps-script - 如何在不更改 URL 的情况下重新发布 Web 应用程序

javascript - Node JS ENOENT - 没有这样的文件或目录

javascript - 发布没有输入字段的静态表单

javascript - AngularJs 多个同名 Controller 在一个文件中隔离作用域

javascript - 我的 Javascript 函数受到另一个已定义的 JS 函数的影响

javascript - 从 Google Sheets 连接到 Bitfinex API

javascript - 异常 : The blob format is unsupported. Google 应用程序脚本

mysql - 无法通过本地主机将谷歌应用程序脚本连接到 mysql