google-apps-script - 编辑单元格时触发 API 调用
问题描述
使用每行都包含一个ID和一个选项列表值的Google 表格。示例:谷歌表格。
我要做的是在有人编辑选项列表单元格时运行自定义函数。该函数接受两个参数,来自同一行的 ID 和选择列表单元格的值,然后执行 HTTP POST 请求以更新我们 CRM 中的记录。
//When STAGE cell on Google Sheet is updated, run this function:
function updateProjectStage(status, id) {
var baseURL = 'https://crm.zoho.com/crm/private/json/Potentials/updateRecords?authtoken=xxx&scope=crmapi&id=', // see docs https://www.zoho.com/crm/help/api/updaterecords.html
recordID = id, // building id from A column
stage = '<Potentials><row no="1"><FL val="Stage">' + status + '</FL></row></Potentials>'; // status from B column
var postURL = baseURL + recordID + '&xmlData=' + stage;
Logger.log(postURL);
var response = UrlFetchApp.fetch(postURL); // update record in crm
var sanitizedResponse = JSON.parse(response.getContentText()); // get confirmation/failure
Logger.log(sanitizedResponse);
}
我不知道如何为这种选择列表类型的单元格运行该函数 - 我不能=updateProjectStage(status, id)
像以前那样只输入单元格,因为它会出错。
示例: 错误消息。
这甚至可能吗?
解决方案
您的答案在于在用户修改工作表上的任何单元格时捕获编辑事件。当然,用户可以修改任何单元格。您的工作是确定该单元格是否在您关心的范围内。onEdit
可以使用此函数捕获事件:
function onEdit(eventObj) {
//--- check if the edited cell is in range, then call your function
// with the appropriate parameters
}
传递给事件的对象描述了被编辑的单元格。所以我们设置了一个“检查范围”,然后将该范围与编辑过的单元格进行比较。这是功能:
function isInRange(checkRange, targetCell) {
//--- check the target cell's row and column against the given
// checkrange area and return True if the target cell is
// inside that range
var targetRow = targetCell.getRow();
if (targetRow < checkRange.getRow() || targetRow > checkRange.getLastRow()) return false;
var targetColumn = targetCell.getColumn();
if (targetColumn < checkRange.getColumn() || targetColumn > checkRange.getLastColumn()) return false;
//--- the target cell is in the range!
return true;
}
编辑事件的完整事件函数将是
function onEdit(eventObj) {
//--- you could set up a dynamic named range for this area to make it easier
var checkRange = SpreadsheetApp.getActiveSheet().getRange("B2:B10");
if (isInRange(checkRange, eventObj.range)) {
//--- the ID cell is on the same row, one cell to the left
var idCell = eventObj.range.offset(0,-1);
//--- the status cell is the one that was edited
var statusCell = eventObj.range;
updateProjectStage(statusCell, idCell);
}
}
这是整个事情:
function isInRange(checkRange, targetCell) {
Logger.log('checking isInRange');
//--- check the target cell's row and column against the given
// checkrange area and return True if the target cell is
// inside that range
var targetRow = targetCell.getRow();
if (targetRow < checkRange.getRow() || targetRow > checkRange.getLastRow()) return false;
Logger.log('not outside the rows');
var targetColumn = targetCell.getColumn();
if (targetColumn < checkRange.getColumn() || targetColumn > checkRange.getLastColumn()) return false;
Logger.log('not outside the columns');
//--- the target cell is in the range!
return true;
}
function onEdit(eventObj) {
//--- you could set up a dynamic named range for this area to make it easier
var checkRange = SpreadsheetApp.getActiveSheet().getRange("B2:B10");
if (isInRange(checkRange, eventObj.range)) {
Logger.log('cell is in range');
//--- the ID cell is on the same row, one cell to the left
var idCell = eventObj.range.offset(0,-1);
//--- the status cell is the one that was edited
var statusCell = eventObj.range;
updateProjectStage(statusCell, idCell);
} else {
Logger.log('must be outside the range');
}
}
function updateProjectStage(status, id) {
Logger.log('we are updating');
}
推荐阅读
- java - 处理流中的 null 而不过滤
- javascript - 从另一个组件有条件地导入一个组件的最佳方法是什么?
- r - 生成可重现随机数的种子
- mysql - 未处理的拒绝 SequelizeConnectionError - Sequelize MYSQL
- apostrophe-cms - 扩展撇号 cms 中常见输入字段的小部件
- ms-word - 使用 Velocity 模板生成更新 TOC 的 Word 文档
- vagrant - 在 CENTOS7 客户机中无法获取 VT-X
- html - 将图像调整为相对 div
- php - 比较 2 个相同的十进制数但给出错误的结果
- jenkins - 如何在 Jenkins groovy 脚本中以编程方式设置身份验证令牌?