Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web applications 阅读电子表格,通过电子邮件发送值_Web Applications_Google Apps Script_Google Sheets - Fatal编程技术网

Web applications 阅读电子表格,通过电子邮件发送值

Web applications 阅读电子表格,通过电子邮件发送值,web-applications,google-apps-script,google-sheets,Web Applications,Google Apps Script,Google Sheets,我是谷歌脚本的初学者,我正在寻找一种从我的电子表格中读取值并用这些值写一封自动电子邮件的可能性。我试图在谷歌和论坛上找到一些信息,但我不是很了解 例如:在我的电子表格中,C2是用户将填写的单元格(它是每平方米的价格计算器)。当C2中填入例如6m2时,D2将计算价格。例如240欧元 现在,我想为这张图纸添加一个脚本,该脚本自动创建并向我发送一封电子邮件,其中包含以下文字:“您想要为‘C2’位置铺地毯。”。这将花费“D2” 我知道这是可能的,但我不知道如何。。。谁能帮帮我吗 通常,您可以使用如下函数

我是谷歌脚本的初学者,我正在寻找一种从我的电子表格中读取值并用这些值写一封自动电子邮件的可能性。我试图在谷歌和论坛上找到一些信息,但我不是很了解

例如:在我的电子表格中,C2是用户将填写的单元格(它是每平方米的价格计算器)。当C2中填入例如6m2时,D2将计算价格。例如240欧元

现在,我想为这张图纸添加一个脚本,该脚本自动创建并向我发送一封电子邮件,其中包含以下文字:“您想要为‘C2’位置铺地毯。”。这将花费“D2”


我知道这是可能的,但我不知道如何。。。谁能帮帮我吗

通常,您可以使用如下函数,该函数在每次编辑单元格时自动激发:

function onEdit(e){
  var range = e.range; //Get edited range
  if (range.getA1Notation() == 'C2'){
    var calculatedValue = range.offset(0,1); //Assumes that the calculated value is in the cell to the right of the cell where the area is entered
    var body = 'You want carpeting for a '+ range.getValue() + ' location. This will cost ' + calculatedValue.getValue();
    try{
      GmailApp.sendEmail('email@yourdomain.com', 'new carpet request', body);
    }
    catch(e)
    {
      Logger.log(e)
    }
  }
}
不幸的是,编辑电子表格时触发onEdit()触发器不允许脚本发送am电子邮件。看

您的下一个最佳选择是将其与一个菜单项相结合,方法是添加一个onOpen函数,如下所示:

function onOpen() {
  SpreadsheetApp.getUi()
      .createMenu('Scripts')
      .addItem('Send Email', 'sendEmail')
      .addToUi();
}
它调用sendEmail函数,如下所示:

function sendEmail(){
  var range = SpreadsheetApp.getActiveSpreadsheet().getRange('C2');
  var calculatedValue = range.offset(0,1);
  var body = 'You want carpeting for a '+ range.getValue() + ' location. This will cost ' + calculatedValue.getValue();
  try{
    GmailApp.sendEmail('email@yourdomain.com', 'new carpet request', body);
  }
  catch(e)
  {
    Logger.log(e)
  }
}

显然,您还需要添加一些错误检查和漂亮的语法,以使您的电子邮件以您喜欢的方式显示,包括单位和所有内容,除非您计划在电子表格中这样做。

哇,太好了,谢谢!它起作用了。我对脚本进行了扩展,以获得更多的值及其工作。谢谢你明确的回答。