如何添加“发送电子邮件"按钮插入Google表格的每一行? [英] How do I add a "Send email" button to each row of a Google sheet?

查看:219
本文介绍了如何添加“发送电子邮件"按钮插入Google表格的每一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google表格,其中包含4列输入内容,用于编写电子邮件

I have a google sheet with 4 columns of input for writing an email

我想(迅速)为每一行(所有添加到D列中)创建一个按钮,该按钮将利用按钮所在行中的信息 only 发送电子邮件

I want to (quickly) create a button for each row (all added in, say, column D) that will send an email utilizing the info only from the row that the button is situated in.

我该怎么做?

  • 如何快速向每行添加多个外观相同的按钮,但以使每个按钮仅触发使用其自身行中的信息来发送电子邮件的方式?

如何编写这样的脚本函数,以便它将捕获数据的行更改为仅作为当前"行?

到目前为止我已经尝试过的:

What I've tried so far:

我已经使用插入">绘图"在单行中添加了一个按钮,并为其指定了自定义函数的名称(sendEmail).

I've added a button to a single row using Insert > Drawing and assigned it the name of a custom function (sendEmail).

我的脚本:

    function sendEmail() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var email = e.values[0];
      var subject = e.values[1];
      var message = e.values[2];
      MailApp.sendEmail(email, subject, message)
    }

  • e.values是访问特定列的最佳方法吗?

    • Is e.values the best way to access specific columns?

      如何添加代码以缩小脚本范围,使其仅可用于其分配的按钮所在的行?

      推荐答案

      您不能使用事件触发器,因为单击图像不会从工作表中获取任何数据.较简单的方法是将行中的活动单元作为目标,并使用Apps Script选择正确的范围.

      You can't use an event trigger because clicking an image doesn't get any data from the sheet. The easier way would be to target the active cell in the row and select the correct range using Apps Script.

      function sendEmail() {
        // define a couple constants for the function
        const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
        const ui = SpreadsheetApp.getUi();
      
        // Make sure the selected row has text
        if(sheet.getActiveCell().isBlank()) {
          ui.alert("Please select a row with a message");
          return;
        }
      
        // if valid, get the correct row of data.
          var row = sheet.getActiveCell().getRow();
      
        // get the range for that row
          var rowRange = sheet.getRange(row, 1, 1, 3).getValues();
      
        // Build the object to use in MailApp
          var address = rowRange[0][0];
          var subj = rowRange[0][1];
          var body = rowRange[0][2];
      
          var msg = "Address: " + address + "\nSubject: " + subj + "\nBody: " + body;
      
        // Here for illustration only
          ui.alert(msg, ui.ButtonSet.OK)
      
          // MailApp.sendEmail(address, subj, body);
      
          // set the send status in col 4
          sheet.getRange(row, 4).setValue("Sent");
      }
      

      这样,您可以在工作表中具有一个图像,并运行一个功能.为每行添加图像会很麻烦.这是轻巧有效的.

      This way, you can have a single image in the sheet which runs a single function. Adding images for each row would get cumbersome. This is lightweight and effective.

      您希望在实际发送消息之前考虑其他一些数据验证步骤,但这应该可以帮助您入门.

      You'd want to think about some other data validation steps before actually sending the message, but this should get you started.

      这篇关于如何添加“发送电子邮件"按钮插入Google表格的每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆