简体   繁体   English

如何在谷歌应用程序脚本中使用setValues屏蔽值?

[英]How can I mask values using setValues in google apps scripts?

I'm using google apps script to construct a custom spreadsheet. 我正在使用谷歌应用程序脚本来构建自定义电子表格。 In order to improve rendering speed of the script I first build an array of output values and then use setValues() to build the spreadsheet all at once. 为了提高脚本的渲染速度,我首先构建一个输出值数组,然后使用setValues()一次性构建电子表格。

When I try to add formulas to into the spreadsheet with setFormulas() it overwrites all my values as well. 当我尝试使用setFormulas()将公式添加到电子表格时,它setFormulas()覆盖我的所有值。 So now I have a case where setValues() overwrites my formulas and setFormulas() overwrites my values. 所以现在我有一个案例,其中setValues()覆盖我的公式, setFormulas()覆盖我的值。

Is there a way I can specify a masking value for specific elements in my output array so that those elements won't overwrite data already in the spreadsheet? 有没有办法可以为输出数组中的特定元素指定掩蔽值,以便这些元素不会覆盖电子表格中已有的数据?

Ideally I would initialize every element of the output array to this masking value, then only the non-masked elements would show up in the spreadsheet. 理想情况下,我会将输出数组的每个元素初始化为此屏蔽值,然后只有非屏蔽元素才会显示在电子表格中。

For anyone that finds this through Google, using setValues() for formulas now seems to work in the new sheets. 对于通过Google发现这一点的任何人来说,现在使用setValues()进行公式似乎可以在新工作表中使用。

More specifically, I'm using code like this to put formulas back in after doing work on the sheet: 更具体地说,我正在使用这样的代码在完成工作表之后重新放入公式:

function setValuesAndFormulas() {
  var sheet = SpreadsheetApp.getActive().getActiveSheet();
  var range = sheet.getDataRange();
  var values = range.getValues();
  var formulas = range.getFormulas();

  for (var i = 0; i < values.length; i++) {
    for (var j = 0; j < values[i].length; j++) {
      if (formulas[i][j].charAt(0) == "=") {
        values[i][j] = formulas[i][j];
      }
    }
  }

  range.setValues(values);
}

Hope that helps! 希望有所帮助!

Unfortunately there isn't support for masking in either the setValues() or setFormulas() method. 不幸的是,在setValues()setFormulas()方法中不支持屏蔽。 If a formula is given to the setValues() method it should set it correctly however, so you may be able to set both the values and formulas in a single call to setValues() . 如果给setValues()方法一个公式,它应该正确设置它,所以你可以在一次调用setValues()设置值和公式。

range.setValues([['foo', '=LEN("bar")']]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM