简体   繁体   English

Google Doc的脚本

[英]Google Doc's Script

Let me start by saying this is my first time using google scripts. 首先我要说这是我第一次使用谷歌脚本。

I have a sheets function I found by searching and after a little editing got functioning. 我有一个通过搜索找到的工作表功能,并在一些编辑功能完成后。 It sends an email about a status update, on a row, based on a menu selection. 它会根据菜单选择发送有关状态更新的电子邮件。 I would also like it to add text to a column titled "Email Sent" (column O if that matters) when the email is sent out. 我还希望在发送电子邮件时将文本添加到标题为“已发送电子邮件”的列(如果重要,则为O列)。

This is the current script: 这是当前的脚本:

function getColIndexByName(colName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == colName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
}

function emailStatusUpdates() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = sheet.getActiveRange().getRowIndex();
  var userEmail = sheet.getRange(row, getColIndexByName("email")).getValue();
  var subject = "Helpdesk Ticket #" + row;
  var body = "We've updated the status of your ticket.\n\nStatus: " + sheet.getRange(row, getColIndexByName("Status")).getValue();
  body += "\n\nNotes: " + sheet.getRange(row, getColIndexByName("Notes")).getValue();
  body += "\n\nResolution: " + sheet.getRange(row, getColIndexByName("Resolution")).getValue();

  MailApp.sendEmail(userEmail, subject, body, {name:"Help Desk"});
}

function onOpen() {
  var subMenus = [{name:"Send Status Email", functionName: "emailStatusUpdates"}];
  SpreadsheetApp.getActiveSpreadsheet().addMenu("Help Desk Menu", subMenus);
}​

This seems pretty trivial. 这看起来很微不足道。 You would use the setValue method on a range in the emailStatusUpdates function. 您可以在emailStatusUpdates函数中的范围上使用setValue方法。

var confirmText = "Email sent at " +
  Utilities.formatDate(new Date(), Session.getScriptTimeZone(),
                       "yyyy-MM-dd HH:mm:ss")
sheet.getRange(row, getColIndexByName("email date")).setValue(confirmText);

I'm using your function getColIndexByName to avoid hard-coding column "O" in the code. 我正在使用你的函数getColIndexByName来避免代码中的硬编码列“O”。 You will have to make sure the header for column O matches what we're passing to this function. 您必须确保列O的标题与我们传递给此函数的标题相匹配。

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

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