简体   繁体   English

不是 Google Apps 脚本中的 function 错误

[英]is not a function error in Google Apps Script

I'm continuing to have trouble with the following google app-script.我继续遇到以下谷歌应用脚本的问题。 Right now it is surfacing on the ranger.activate() line.现在它出现在 ranger.activate() 线上。 When run the following error shows up in my log:运行时,我的日志中显示以下错误:

[20-06-20 08:16:26:262 PDT] TypeError: ranger.activate is not a function at createDocFromRow(Code:14:8) [20-06-20 08:16:26:262 PDT] TypeError:ranger.activate 不是 createDocFromRow 的 function(代码:14:8)

What this code is trying to do is take a selected row in a Google Sheet and turn it into a data object (this happens in more steps than is needed, but I'm breaking it down granularly to try and find the problem. Once found i can amalgamate steps and reduce VAR calls).这段代码试图做的是在谷歌表格中选择一个行并将其转换为数据 object (这发生的步骤比需要的要多,但我正在细化它以尝试找到问题。一旦找到我可以合并步骤并减少 VAR 电话)。

Once the data object is created, I parse out the data I need and drop it into a Google Doc template.创建数据 object 后,我会解析出所需的数据并将其放入 Google Doc 模板中。

Any help identifying the root cause of this "is not a function" error would go a long way to my mental health improvement.任何帮助确定这个“不是功能”错误的根本原因都会使我的心理健康得到很大改善。

function createDocFromRow(){
var templateid = "1UjKBk0SyNUlswiWxXm3oCpEg3-RsJCPhTwwnyjif58s"; // Label Copy Doc ID
var FOLDER_NAME = "Label Copy"; // folder name of where to put completed Docs
var FILENAME = "LabelCopy" // To save new files as
// get the data from a ROW selected by Spreadsheet user
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var ranger = sheet.getSelection().getActiveRange;
ranger.activate();
ss.toast(ranger);
var dataArray = ranger.getValues();
// dataArray is my object[][]
var songname = dataArray[0][2];
var songwriters = dataArray[0][3];
var publishers =  dataArray[0][4];
var artist = dataArray[0][6];
var useremail = dataArray[0][7];
// create a new Label Copy Document 
var docid = DocsList.getFileById(templateid).makeCopy().getId();
var doc = DocumentApp.openById(docid);
var body = doc.getActiveSection();
// replace text FROM dataArray into placeholder space
body.replaceText("##SONG NAME##", songname);
body.replaceText("##artist##", artist);
body.replaceText("##user email##", useremail);
body.replaceText("##Timestamp##", Utilities.formatDate(row[1], "GMT", "HH:mm dd/MM/yyyy"));
body.replaceText("##Songwriters##", songwriters);
body.replaceText("##Publishers##", publishers);
//Prepare to close and rename file.
var folder = DocsList.getFolder(FOLDER_NAME);
file.addToFolder(folder);
doc.setName(LABELCOPY+artists)
doc.saveAndClose();
// message Sheets user
ss.toast("Label Copy Document Created");
}

It should be它应该是

var ranger = sheet.getSelection().getActiveRange();
ranger.activate();

You are missing () for getActiveRange您缺少getActiveRange()

Explanation解释

getActiveRange is a Function which returns an instance of Range Class. getActiveRange是一个Function ,它返回一个Range Class 的实例。 And a function needs to be invoked using parentheses ()并且需要使用括号()调用 function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM