如何通过api基于插入行触发Google Apps脚本功能

Gel*_*Ram 5 google-apps-script

  1. 我有一个包含5列的Google表格(名字,地址,SKU,报价,状态).

  2. 我有一个应用程序脚本函数(createQuote),它从google工作表行查看上述变量的值,并创建一个google文档引用,将变量替换为值.

  3. 我使用Zapier在我上面的google工作表中插入行.

正在努力的方面 - :当通过zapier(Google Sheet API调用)插入新行时,我需要一种方法来触发我的createQuote函数.

我试过玩触发器,但无法做到,任何帮助表示赞赏.

谢谢

这是我的功能代码 -

function quoteCreator(){

docTemplate = "googledocidgoeshere"
docName = "Proposal"

  var sheet = SpreadsheetApp.getActive().getSheetByName("Main")
  var values = sheet.getDataRange().getValues()
  var full_name = values[1][0]

  var copyId   = DriveApp.getFileById(docTemplate).makeCopy(docName+" for "+full_name).getId()


  // Open the temporary document
 var copyDoc  = DocumentApp.openById(copyId);
// Get the document’s body section
 var copyBody = copyDoc.getActiveSection();
// Replace place holder keys/tags,  
 copyBody.replaceText("keyFullName", full_name);
 copyDoc.saveAndClose();
// Convert temporary document to PDF by using the getAs blob conversion
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");

// put the link of created quote in the quote column
  var url = DocumentApp.openById(copyId).getUrl()
 var last = sheet.getRange(2, 7, 1, 1).setValue(url)


 }
Run Code Online (Sandbox Code Playgroud)

注意:我还没有把循环放在上面,我会按照我的要求开始工作.

小智 5

通过Sheets API或Apps Script进行的更改不会触发onEdit触发器.我给出了两个解决方法.

网络应用

如果有任何进程更新,表单还会向您的脚本发送GET或POST请求,并将其部署为Web应用程序.例如,GET版本可能会访问https://script.google.com/.../exec?run=quoteCreator

function doGet(e) {
  if (e.parameter.run == "quoteCreator") {
    quoteCreator();
    return ContentService.createTextOutput("Quote updated"); 
  }
  else {
    return ContentService.createTextOutput("Unrecognized command");
  } 
} 
Run Code Online (Sandbox Code Playgroud)

Web应用程序的发布方式应使您的其他进程能够执行上述操作; 通常这意味着"每个人,甚至是匿名的".如果安全性是一个问题,添加令牌参数可能有所帮助,例如,URL将具有&token=myTokenmyToken是webapp将使用的字符串e.parameter.token.

这里使用GET方法进行说明,您可能会发现POST对此操作更有意义.

重要提示:当GET或POST请求触发执行时,这些方法getActive...不可用.您需要使用其ID或网址打开所需的任何电子表格(请参阅openById,openByUrl).

定时触发器

按时间间隔(例如,每5分钟)运行一个函数,该函数检查工作表中的行数并quoteCreator在需要时触发.该函数checkNewRows在"脚本属性"中存储非空行数,因此可以检测到更改.

function checkNewRows() {
  var sp = PropertiesService.getScriptProperties();
  var oldRows = sp.getProperty("rows") || 0; 
  var newRows = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Main").getLastRow();
  if (newRows > oldRows) {
    sp.setProperty("rows", newRows);
    quoteCreator();
  } 
}
Run Code Online (Sandbox Code Playgroud)