Using built-in spreadsheet functions in a script

前端 未结 3 1108
情话喂你
情话喂你 2020-12-01 23:45

I\'m using Google App Script for the first time. I\'m using it on a Google Doc spreadsheet.

I\'m trying very simple functions, just to learn the basics. For example

相关标签:
3条回答
  • 2020-12-02 00:01

    Google Apps Script still does not (1/7/20) include an API to Google Sheets native functions.

    But you can set the formula (native functions) of a cell named as a named range in a spreadsheet.

    Then in the GAS:

    var nativeOutput = spreadsheet.getRangeByName("outputCell").getValue();
    

    Voila! Your GAS is calling the native function in the cell.

    You can send data from the GAS to the native function in the cell, by naming another cell in the sheet (or in any sheet) referred to by the formula in the other cell:

    spreadsheet.getRangeByName("inputCell").setValue(inputData);
    

    Your GAS can dynamically create these cells, rather than hardcoding them, eg:

    // Create native function, its input and output cells; set input value; use native function's output value:
    
    
    // Use active spreadsheet.
    var spreadsheet = SpreadsheetApp.getActive();
    
    
    // Name input, output cells as ranges.
    spreadsheet.setNamedRange("inputCell", spreadsheet.getRange("tuples!F1"));
    spreadsheet.setNamedRange("outputCell", spreadsheet.getRange("tuples!F2"));
    
    var outputCell = spreadsheet.getRangeByName("outputCell");
    var inputCell = spreadsheet.getRangeByName("inputCell");
    
    
    // Set native formula that consumes input cell's value, outputting in formula's cell.
    outputCell.setFormula("=WEEKNUM(inputCell)");
    
    
    // Call native function by setting input cell's value for formula to consume.
    // Formula sets its cell's value to formula's output value.
    inputCell.setValue(15);
    
    // Consume native function output.
    var nativeOutput = outputCell.getValue();
    Logger.log("nativeOutput: "+ JSON.stringify(nativeOutput)); // Logs "nativeOutput: 3"
    

    Beware: this technique exposes the code in cells that a spreadsheet user can access/change, and other spreadsheet operations could overwrite these cells.

    0 讨论(0)
  • 2020-12-02 00:16

    What the spreadsheet functions can do, Javascript can do. I just have to replace var day_num = weekday() by var day_num = new Date(date).getDay()

    Here is the result :

    /**
    * Writes the day of the week (Monday, Tuesday, etc), based on a date
    */
    function day_name(date) {
      // calculate day number (between 1 and 7)
      var day_num = new Date(date).getDay();
    
      // return the corresponding day name
      switch(day_num) {
        case 0: return 'Sunday';    break;
        case 1: return 'Monday';    break;
        case 2: return 'Tuesday';   break;
        case 3: return 'Wednesday'; break;
        case 4: return 'Thursday';  break;
        case 5: return 'Friday';    break;
        case 6: return 'Saturday';  break;
      }
      return 'DEFECT - not a valid day number';
    };
    
    0 讨论(0)
  • 2020-12-02 00:21

    Google Apps Script is a subset of JavaScript, spreadsheet functions are currently not supported. For example, if you want to create a function that returns today's date you should write :

    function test_today(){
    return new Date()
    }// note that this will  eventually return a value in milliseconds , you'll have to set the cell format to 'date' or 'time' or both ;-)
    

    syntax is the same as with sheet functions : =test_today() see tutorial

    There are many internet ressources on javascript, one of the most useful I found is w3school

    0 讨论(0)
提交回复
热议问题