Use existing spreadsheet formulas in a custom formula in google docs/spreadsheets

前端 未结 2 1910
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 10:20

I like writing my own formulas inside of Google Docs Spreadsheets. But often what I want to do is very similar to a function that already exists. As an example,

2条回答
  •  孤城傲影
    2021-02-06 11:16

    In your custom appscript, you can use the in-built formulas of google spreadsheet in this way:

    Lets say you want to use =WEEKDAY() function on cell A1.

    Then, get your active spreadsheet like this in your custom appscript function:

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("YOUR_SHEET_NAME");
    

    now, set the formula like this:

    sheet.getRange("A1").setValue("=WEEKDAY()");
    

    Also, if you want to convert 0,1 etc to Sunday,Monday...then define an array like this:

    var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    

    And then use:

    var dayIndex = sheet.getRange("A1").getValue(); 
    Logger.log(days[dayIndex]);
    

    You can see the logs using ctrl+enter or by going to View->Logs in script editor itself.

提交回复
热议问题