在脚本问题中获取谷歌表单标题和值

时间:2015-03-30 16:38:03

标签: javascript google-apps-script google-sheets google-spreadsheet-api google-form

我希望从谷歌表单回复中的先前提交的行中获取一些值,然后将这些值以及表单的标题发送给最终用户。

然而,我认为我可能会采用错误的方式,我可以拉出电子表格和工作表并使用它来编辑一个字段,但我看不出如何从这里拉出值,它只是回归“范围”。从范围中获取值的正确语法是什么?

有没有办法拉出链接表格的标题?或电子表格,如果这是不可能的,因为这只是“formName”+(响应)。 enter image description here

这是我到目前为止的代码,它可能只是缺少一些简单的东西(它接受的e值是格式化的URL):

function doGet(e){
  var params = JSON.stringify(e);
  var message = "thank you, your Decision has been sent.";



  var id = '10kf5QDeBc-vr6XJnkFxQh-ch_poQhqBQWQbgJ';
 var ss = SpreadsheetApp.openById(id);
 // var ss = SpreadsheetApp.getActiveSpreadsheet();
 // var sheet = ss.getActiveSheet();
var sheet = ss.getSheets()[0];
var column = 20; 
var datecolumn = 5;
var row = e.parameter.row;
//var title = sheet.getTitle();
var sheetname = ss.getSheetName();
var sheetnamesheet = sheet.getName();
 var eventDaterange = sheet.getRange(row, datecolumn);
 var eventDate = eventDaterange.getvalues();


 var approval_cell = sheet.getRange(row, column);



//set default to declined
  approval_cell.setValue("declined");

  var approvedMessage = "<HTML><body>"+
              "<h2>your request has been Approved </h2><br />"
 +"<P>" + title +"   " + sheet
 +"<p>" +  "  for this date:   "+ eventDate;

  var deniedMessage = "<HTML><body>"+
              "<h2>your request has been denied </h2><br />"
  +"<P>" + ss +"   " + sheet
  +"<p>" +  "  for this date:   "+ eventDate;

  var aprovalResponce = (e.parameter.approval == 'true') ? approvedMessage :     deniedMessage;

  //var msg = "Your manager said this:  " + aprovalResponce;
  var msg = aprovalResponce
 var replyEmail = e.parameter.reply;



   if (e.parameter.approval == 'true') {
     approval_cell.setValue("APPROVED");

  } else {
    approval_cell.setValue("DECLINED");

  }


  //send the actual email out
  //MailApp.sendEmail(replyEmail, "Approval Request", msg);
  MailApp.sendEmail(replyEmail, "Approval Request", {htmlBody: msg});

  return HtmlService.createHtmlOutput(message);
}

1 个答案:

答案 0 :(得分:1)

要从范围中获取值,请使用var values = range.getValues();这将在2D数组值中返回结果,这类似于表示范围的网格,即值[row] [column]。所以,如果我们的范围是“A1:C3”,我们想要A1和C3中的值,我们可以这样做:

var range = sheet.getRange("A1:C3");
var values = range.getValues();
var valueOne = values[0][0];
var valueTwo = values[2][2];

var msg = '"Form name" + valueOne + ValueTwo'

我不知道如何获取表单名称,也许就像e.source.title一样简单?如果没有,如果您使用多个表单,则可能必须通过从提交的表单中获取表单ID,然后使用switch语句将该ID与脚本中的表单名称列表进行匹配来执行此操作。