【问题标题】:Google script function optimization with more than 1 input value1个以上输入值的谷歌脚本功能优化
【发布时间】:2017-12-28 14:17:17
【问题描述】:

我已经成功地对我的一些只需要 1 个输入范围的谷歌脚本函数进行了优化。

我试图将 1 个数组(姓名)和 2 个值(日期和学校名称)或 3 个数组作为输入。

代码如下:

  // input - array of names like this 'Jon Snow'
  // date - date with format like this '2017-12-28'
  // school name like this 'School name'
  function Allanswers(input, date, school) {

  if (input.map) {            // Test whether input is an array.
    return input.map(function(x, date, school){ // Recurse over array if so.
      return Allanswers(x, date, school)
    }); 
  } else {

  return date; // returns 0
  // the function is much more complicated but with this line I am checking if the value of date is taken from google spreadsheet
  }
  }

【问题讨论】:

    标签: javascript optimization google-apps-script google-sheets custom-function


    【解决方案1】:

    问题是我对 .map 了解不多

    我已经阅读了关于谷歌脚本“Going GAS”的非常好的书的一些部分。

    我的问题的答案是这样的:

    // this function can be used with vertical input array and horizontal date and school arrays or values
    function Allanswers(input, date, school ) {
    if ( !Array.isArray(date) ) {
      return input.map (function (d, i) { 
        return process (d[0], date, school)        
    })   
    }
    else {
    return input.map (function (d, i) { 
     return date[0].map (function (k, h) {
      return process (d[0], k, school[0][h])   
      })
    
    })
    }
    function process(teacher, day, place) {
    
      // your calculations
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 2021-11-20
      相关资源
      最近更新 更多