Apps脚本:如何将Google Sheet文件通过电子邮件发送到HTML服务输入中指定的电子邮件?

时间:2018-02-13 18:20:00

标签: html google-apps-script

我正在我的Google表格文件中构建一个HTML服务用户界面,允许用户在HTML表单的弹出对话框中键入目标收件人电子邮件地址。我的代码目前无效,因为运行时没有生成电子邮件。

JS代码:

    function openDialog() {
      var html = HtmlService.createHtmlOutputFromFile('Index');
      SpreadsheetApp.getUi()
      .showModalDialog(html, 'Email?');
    }

    function sendEmail() {
    var to = document.getElementbyID("emailInput");
      MailApp.sendEmail( {
        email: to,
        subject: "good morning",
     })
     }

HTML:

<html>
  <head>
    <base target="_top">
</head>
 <body>
      <form id="emailForm">
      Send to Email Address:<br> <input type="email" id="emailInput" 
      name="email" size="40"/><br>
      <button onClick="sendEmail()">Send</button>
      </form>
 </body>
</html>

2 个答案:

答案 0 :(得分:1)

您无法直接从客户端调用服务器端功能。您需要使用google.script.run API在两者之间进行通信。请尝试以下方法:

// Inside your Apps Script .gs code

// listen for an object sent from the HTML form
function sendEmail(formObj) {

  // Extract the email address submitted
  var to = formObj.email;

  // Send the email
  MailApp.sendEmail(to,"good morning", "The email body")
}
...
<body>
  <form id="emailForm">
      <p>Send to Email Address:</p>
      <br> 
      <input type="email" id="emailInput" name="email" size="40"/> <!-- "name" becomes the key in formObject -->
      <br>
      <input type="button" value="Send Email" onclick="google.script.run.sendEmail(this.parentNode)" /> <!-- google.script.run allows you to call server-side functions -->
      </form>
</body>
...

答案 1 :(得分:0)

抱歉,我没有足够的声誉发表评论,但我对Brian的代码进行了2次小修改,这将有所帮助:

首先将this.form更改为<input type="button" value="Send Email" onclick="google.script.run.sendEmail(this.form)

MailApp.sendEmail( {
   to: to,
   subject: "good morning",
})

然后更新MailApp.sendEmail函数的收件人的语法:

library(shiny)
library(quantreg)
library(quantregGrowth)
library(plotly)
library(rsconnect)
library(ggplot2)
library(lattice)

    ui = tagList(
      tags$head(tags$style(HTML("body{ background: aliceblue; }"))),
      navbarPage(title="",
                 tabPanel("Data Import",
                          sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
                                                      tags$hr(),
                                                      h5(helpText("Select the read.table parameters below")),
                                                      checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                                                      checkboxInput(inputId = "stringAsFactors", "StringAsFactors", FALSE),
                                                      radioButtons (inputId = 'sep', label = 'Separator', 
                                                                    choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
                          ),
                          mainPanel(uiOutput("tb1"))
                          )),
                 tabPanel("Interval",
                          sidebarLayout(sidebarPanel(
                            uiOutput("model_select"),
                            uiOutput("var1_select"),
                            uiOutput("rest_var_select"),
                            #uiOutput("testText1"), br(),
                            #textInput("Smooting Parameter min value", "Smooting Parameter max value", value = "")                        
                            sliderInput("range", "Smooth Parameter range:",min = 0, max = 1000, value = c(0,100)),
                            downloadButton('downloadData', 'Download')
                          ),
                          mainPanel(helpText("Selected variables and Fitted values"),
                                    verbatimTextOutput("other_val_show")))),
                 tabPanel("Model Summary", verbatimTextOutput("summary")), 
                 tabPanel("Scatterplot", plotOutput("scatterplot"))#, # Plot
                 #tabPanel("Distribution", # Plots of distributions
                 #fluidRow(
                 #column(6, plotOutput("distribution1")),
                 #column(6, plotOutput("distribution2")))
                 #)             
                 ,inverse = TRUE,position="static-top",theme ="bootstrap.css"))