从同一个脚本文件中调用另一个函数

duc*_*uck 4 function google-apps-script

在这篇文章中,Serge 回答说:“当然,您可以使用项目内不同脚本文件中的任何函数”。

这样做的协议是什么?

我有一个要在三个不同脚本中使用的函数。它非常密集,我正在不断完善它,所以我不想在函数之间复制和粘贴所有代码。这些文件都在同一个项目中。Serge 在此链接上的回复是我所能确认的最接近的回复,但它没有提供协议。

如何从一个 Google Apps 脚本调用另一个函数?

更具体地说,我有一个函数可以删除文件、抓取模板、复制它、重命名它,然后用电子表格中的当前数据填充它。我想在几个电子邮件脚本中运行该功能,这些脚本在不同时间发送给不同的人,发送不同的消息。

谢谢!

MOGSDAD 回应后更新:

我有 5 个 .gs 文件,都在同一个脚本项目中(就像您在示例中显示的两个 .gs 文件一样。一个创建了一个新文件 (newFile.gs),其他的则打算在不同时间通过电子邮件将其发送给不同的受众.gs 文件之一的示例如下所示:

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);

}
Run Code Online (Sandbox Code Playgroud)

如您所见,我不知道在第二行键入什么内容才能让 newFile.gs 作为 emailNewDoc.gs 的一部分运行。对不起,我学究了。我是个新手,但认为如果我能弄清楚基础知识,谷歌脚本将永远改变我的生活。

*** 在 SERGE 的评论后编辑

Serge,这是原始 .gs 文件中的代码

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"));
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我定义了 URL,这就是我想拉入另一个函数的变量,以便我可以通过电子邮件将其发送出去。

再次感谢!

小智 7

我想我明白你问的问题。这是一个示例项目,其功能用于设置我的解释:

scriptOne.gs:

  • 函数A();
  • 函数B();
  • 函数C();

scriptTwo.gs:

  • 函数D();
  • 函数E();
  • 函数F();

在 scriptOne.gs 中,我可以通过执行以下操作来调用 functionD():

functionA() {
  functionD();
}

functionB() {
}

functionC() {
}
Run Code Online (Sandbox Code Playgroud)

我知道这看起来非常基本,但我使用这个简单的命名法来调用其他项目的函数。这不适合你吗?如果您可以提供一个清晰的示例来说明您要实现的目标,我可以尝试为您提供更好的答案。

如果您尝试在一行中运行 scriptTwo.gs 中的所有函数,那么我将runScriptTwo()在 scripTwo.gs 中创建一个函数,该函数调用您的 scriptTwo.gs 文件中的所有函数。然后你可以runScriptTwo()在我的第一个代码中调用你的,而不是调用functionD(). 那有意义吗?


Ser*_*sas 3

只是另一个答案来澄清你的最后一点:

这是调用另一个函数的代码:

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} );

}
Run Code Online (Sandbox Code Playgroud)

并在另一个函数中(最终在同一项目的另一个 .gs 文件中更改函数的结尾):

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
}
Run Code Online (Sandbox Code Playgroud)