最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do i find maximum of values in google script which are sums of submitted values from a form now stored in sheet

programmeradmin5浏览0评论

I have a google form with 24 questions. I need to get the sum of certain questions and then find the maximum value however using max() doesn't work. How can I work out the maximum value of a set of variables that are defined within the script I am writing.

Finding the maximum value is needed as text is inserted into a document depending on the highest value of the scores submitted.

function onFormSubmit(e) { // add an onsubmit trigger
// Gather data from submitted values in the form
   var date_stamp = e.values[0];
   var email_address = e.values[1];
   var full_name = e.values[2];
   var q1 = e.values[3];
   var q2 = e.values[4];
   var q3 = e.values[5];
   var q4 = e.values[6];
   var q4 = e.values[7];
   var q5 = e.values[8];
   var q6 = e.values[9];
   var q7 = e.values[10];
   var q8 = e.values[11];
   var q9 = e.values[12];
   var q10 = e.values[13];
   var q11 = e.values[14];
   var q12 = e.values[15];
   var q13 = e.values[16];
   var q14 = e.values[17];
   var q15 = e.values[18];
   var q16 = e.values[19];
   var q17 = e.values[20];
   var q18 = e.values[21];
   var q19 = e.values[22];
   var q20 = e.values[23];
   var q21 = e.values[24];
   var q22 = e.values[25];
   var q23 = e.values[26];
   var q24 = e.values[27];

// Add the scores for the categories and areas  
   var scoreInputs = q1+q4+q7+q10+q13+q16+q19+q22;
   var scoreProcessing = q2+q5+q8+q11+q14+q17+q23;
   var scoreOutputs = q3+q6+q9+q12+q15+q18+q21+q24;

  var largestScore = max(scoreInputs, scoreProcessing, scoreOutputs);
  if (largestScore == scoreInputs) {
// paragraph for Input as highest score
    var knowledgePara = "input positive text"; 
  } else if (largestScore == scoreProcessing) {
// paragraph for Processing as highest score
    var knowledgePara = "processing positive text"; 
  } else if (largestScore == scoreOutputs) {
// paragraph for Outputs as highest score
    var knowledgePara = "outputs positive text"; 
  };

I have a google form with 24 questions. I need to get the sum of certain questions and then find the maximum value however using max() doesn't work. How can I work out the maximum value of a set of variables that are defined within the script I am writing.

Finding the maximum value is needed as text is inserted into a document depending on the highest value of the scores submitted.

function onFormSubmit(e) { // add an onsubmit trigger
// Gather data from submitted values in the form
   var date_stamp = e.values[0];
   var email_address = e.values[1];
   var full_name = e.values[2];
   var q1 = e.values[3];
   var q2 = e.values[4];
   var q3 = e.values[5];
   var q4 = e.values[6];
   var q4 = e.values[7];
   var q5 = e.values[8];
   var q6 = e.values[9];
   var q7 = e.values[10];
   var q8 = e.values[11];
   var q9 = e.values[12];
   var q10 = e.values[13];
   var q11 = e.values[14];
   var q12 = e.values[15];
   var q13 = e.values[16];
   var q14 = e.values[17];
   var q15 = e.values[18];
   var q16 = e.values[19];
   var q17 = e.values[20];
   var q18 = e.values[21];
   var q19 = e.values[22];
   var q20 = e.values[23];
   var q21 = e.values[24];
   var q22 = e.values[25];
   var q23 = e.values[26];
   var q24 = e.values[27];

// Add the scores for the categories and areas  
   var scoreInputs = q1+q4+q7+q10+q13+q16+q19+q22;
   var scoreProcessing = q2+q5+q8+q11+q14+q17+q23;
   var scoreOutputs = q3+q6+q9+q12+q15+q18+q21+q24;

  var largestScore = max(scoreInputs, scoreProcessing, scoreOutputs);
  if (largestScore == scoreInputs) {
// paragraph for Input as highest score
    var knowledgePara = "input positive text"; 
  } else if (largestScore == scoreProcessing) {
// paragraph for Processing as highest score
    var knowledgePara = "processing positive text"; 
  } else if (largestScore == scoreOutputs) {
// paragraph for Outputs as highest score
    var knowledgePara = "outputs positive text"; 
  };
Share Improve this question edited Jul 18, 2018 at 17:55 Wicket 38.7k9 gold badges80 silver badges195 bronze badges asked Jul 18, 2018 at 14:16 Daniel CohenDaniel Cohen 111 gold badge1 silver badge4 bronze badges 2
  • What part of your custom defined max function isn't working? Or did you mean to use the native JavaScript implementation of max? Did you do any research on how one uses max in JavaScript? – tehhowch Commented Jul 18, 2018 at 14:21
  • i didn't define max. I was trying to use the native JavaScript max. Naturally i researched it but I didn't realise i had to use max() as a method of Math. Math.max() works a treat. – Daniel Cohen Commented Jul 19, 2018 at 2:51
Add a ment  | 

2 Answers 2

Reset to default 3

You need to use Math.max().

var largestScore = Math.max(scoreInputs, scoreProcessing, scoreOutputs);

Keep in mind that your current logic might not correctly handle cases where the largest score is held by multiple values.

It's very likely that you assumed that spreadsheet functions can be used on Apps Script.

  • Unfortunately that assumption is wrong
  • Fortunately we could use the methods of the built-in JavaScript object Math, in this particular case Math.max()

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论