【问题标题】:Return Absolute Value of a Google Sheet cell with AppScript使用 AppS 脚本返回 Google 表格单元格的绝对值
【发布时间】:2021-09-11 11:53:30
【问题描述】:

在 Google 表格中,我尝试使用应用脚本返回单元格的绝对值。到目前为止,我有以下代码:

var SEARCH_COL_IDX = 1; // variable that indicates which column to search, 0 = column A
function SearchInventario() {

var ss      = SpreadsheetApp.getActiveSpreadsheet();
var formS   = ss.getSheetByName("EntryFrmMovimiento"); //Form sheet that captures data

var str     = formS.getRange("E4").getValue(); //cell that contains search criteria
var values= ss.getSheetByName("DataInventario").getDataRange().getValues();//Tab with dataset
for (var i = 0; i < values.length; i++) {
    var row = values[i];
    if (row[SEARCH_COL_IDX] == str) {

    formS.getRange("C10").setValue(row[0]);
    formS.getRange("C11").setValue(row[1]);//This is the cell for which I need Absolute value
   }}}

它可以很好地返回数据集中的数据,但是我需要将单元格 C11 转换为此选项卡中的绝对值,而不是原始数据集中的绝对值。

我们将不胜感激任何点击、提示或想法!

【问题讨论】:

    标签: google-apps-script google-sheets absolute-value


    【解决方案1】:

    仅用于教育目的。您可以随时制作自己的Math.abs() FUNction:

    formS.getRange("C11").setValue( row[1] < 0 ? -row[1] : row[1] );
    

    const abs = x => x < 0 ? -x : x;
    
    console.log(abs(-25)) // = 25
    console.log(abs(5))   // = 5
    console.log(abs(0))   // = 0

    甚至更有趣:

    const abs = x => Number(x.toString().replace('-',''))
    
    console.log(abs(-25))   // = 25
    console.log(abs(-1.25)) // = 1.25
    console.log(abs(5))     // = 5
    console.log(abs(0))     // = 0

    【讨论】:

    • 非常感谢您的贡献!我一定会尝试的!干杯!
    【解决方案2】:

    试试这个:

    formS.getRange("C11").setValue(Math.abs(row[1]));
    

    参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      相关资源
      最近更新 更多