从同一脚本文件中调用另一个函数 [英] Call another function from within same script file
问题描述
Serge回答:但是,您当然可以使用项目中不同脚本文件中的任何功能."
On this post, Serge replies: "but you can, of course, use any function from different script files inside a project".
执行此操作的协议是什么?
What is the protocol to do that?
我有一个函数要在三个不同的脚本中使用.它非常密集,我正在不断完善它,所以我不想在函数之间复制和粘贴所有代码.这些文件都在同一个项目中. Serge在此链接上的回复是我能证实的最接近的回答,但没有给出协议.
I have a function that I want to use in three different scripts. It's pretty dense and I'm continually refining it, so I don't want to copy and paste all the code between functions. The files are all within the same project. Serge's response at this link is the closest I can come to confirm it's possible, but it doesn't give the protocol.
更具体地说,我有一个删除文件,获取模板,复制,重命名并用电子表格中的当前数据填写文件的功能.我想在几个发送电子邮件的脚本中运行该功能,这些脚本在不同时间以不同的消息发送给不同的人.
To be more specific, I have a function that deletes a file, grabs a template, copies it, renames it, and fills it out with the current data in a spreadsheet. I want to run that function within several emailing scripts that go out at different times, with different messages, to different people.
谢谢!
在MOGSDAD响应后更新:
UPDATED AFTER MOGSDAD RESPONSE:
我有5个.gs文件,它们都在同一个脚本项目中(就像您在示例中显示的两个.gs文件一样.一个文件创建了一个新文件(newFile.gs),其他文件则打算在不同时间通过电子邮件发送该文件.gs文件之一的示例如下所示:
I have 5 .gs files, all within the same script project (like the two .gs files you showed in your example. One creates a new file (newFile.gs), the others are intended to email it at various times to various audiences. An example of one of the .gs file would look like this:
function emailNewDoc() {
//Here's where I want to run the script newFile.gs so that the new doc is created before the mail goes out
var message = "Hi,";
message += '<a href='+url+'>Here's the new document</a>' //url is defined in newDoc.gs after the new document is created
var subject = "The updated document"
var recipients = "xyz@xyz.com"
MailApp.sendEmail(recipients, subject, message);
}
如您所见,我不知道要在第二行键入什么来使newFile.gs作为emailNewDoc.gs的一部分运行.很抱歉要学究.我是一个新手,但是如果我能掌握基础知识,那么Google脚本将永远改变我的生活.
As you can see, I don't know what to type on the second line to get the newFile.gs to run as part of emailNewDoc.gs. Sorry to be pedantic. I'm a total novice, but think google scripts is going to change my life forever if I can figure out the basics.
***编辑服务后的评论
***EDIT AFTER SERGE'S COMMENT
Serge,这是原始.gs文件中的代码
Serge, here's the code in the original .gs file
function newFile() {
var docTemplate = "[insert template doc key here]"; //this one uses the document key, not the file name like mine did
var copy = DocsList.getFileById(docTemplate).makeCopy("my DOCUMENT");
var url = copy.getUrl();
var copyId = copy.getId();
var copyDoc = DocumentApp.openById(copyId);
var body = copyDoc.getActiveSection();
body.replaceText('{date}', Utilities.formatDate(new Date(sheet.getRange('A1').getValues()), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "MMM dd"));
}
如您所见,我定义了URL,这是我想拉入另一个函数的变量,因此我可以通过电子邮件发送出去.
As you can see, I define URL, and that's the variable I want to pull into another function so I can email it out.
再次感谢!
推荐答案
只是另一个答案,以澄清您的最后一点:
Just another answer to clarify your last point :
这是您的调用另一个函数的代码:
Here is your code that calls the other function :
function emailNewDoc() {
var url = newFile(); // this is calling the other function and gets the value you need from it
var message = "Hi,";
message += '<a href='+url+'>Here's the new document</a>' //url is defined in newDoc.gs after the new document is created
var subject = "The updated document"
var recipients = "xyz@xyz.com"
MailApp.sendEmail(recipients, subject, 'please read html body',{htmlBody : message} );
}
和其他功能(最终在同一项目的另一个.gs文件中,更改功能的结尾):
and in the other function (eventually in another .gs file in the same project change the end of your function) :
function newFile() {
var docTemplate = "[insert template doc key here]"; //this one uses the document key, not the file name like mine did
var copy = DocsList.getFileById(docTemplate).makeCopy("my DOCUMENT");
var url = copy.getUrl();
var copyId = copy.getId();
var copyDoc = DocumentApp.openById(copyId);
var body = copyDoc.getActiveSection();
body.replaceText('{date}', Utilities.formatDate(new Date(sheet.getRange('A1').getValues()), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "MMM dd"));
return url;// this returns the value you need in the other function
}
这篇关于从同一脚本文件中调用另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!