运输不在同一行的数值范围

huangapple go评论67阅读模式
英文:

Transporting ranges of values that are not in the same line

问题

我有这段代码,它获取一些范围,然后逐一获取其中的值。我尝试使用GetRangeList来缩短它,但在使用SetValues时出现了问题,因为它们变成了字符串。代码实际上可以工作,它可以完成需要做的一切,但我正在尝试优化它。有没有办法?
我还尝试过将范围指定为矩阵,比如“E12:G15”,但它不会转置成一整行,它会将其视为与源相等。

var dados1 = origem.getRange("E12:G12");
var source1 = dados1.getValues();
var dados2 = origem.getRange("E13:G13");
var source2 = dados2.getValues();
var dados3 = origem.getRange("E14:G14");
var source3 = dados3.getValues();
var dados4 = origem.getRange("E15:G15");
var source4 = dados4.getValues();
sheetBase.getRange(sheetBase.getLastRow()+1, 1, 1, 3).setValues(sourceEqpt);
sheetBase.getRange(sheetBase.getLastRow(), 4, 1, 3).setValues(source1);
sheetBase.getRange(sheetBase.getLastRow(), 7, 1, 3).setValues(source2);
sheetBase.getRange(sheetBase.getLastRow(), 10, 1, 3).setValues(source3);
sheetBase.getRange(sheetBase.getLastRow(), 13, 1, 3).setValues(source4);
英文:

I have this code bit where it gets some ranges and the get the values inside them, one by one. I've tried to use GetRangeList to shorten it, but i had issues using SetValues along it because they turned out to be a string. The code actually works, it does everything it needs to do, but i'm trying to optmize it. Is there any way?
I've also tried to specify the range as a matrix, like "E12:G15", but then it doesn't transpose to a whole line, it reads it as equal to the source.

  var dados1 = origem.getRange("E12:G12");
  var source1 = dados1.getValues();
  var dados2 = origem.getRange("E13:G13");
  var source2 = dados2.getValues();
  var dados3 = origem.getRange("E14:G14");
  var source3 = dados3.getValues();
  var dados4 = origem.getRange("E15:G15");
  var source4 = dados4.getValues();
  sheetBase.getRange(sheetBase.getLastRow()+1, 1, 1, 3).setValues(sourceEqpt);
  sheetBase.getRange(sheetBase.getLastRow(), 4, 1, 3).setValues(source1);
  sheetBase.getRange(sheetBase.getLastRow(), 7, 1, 3).setValues(source2);
  sheetBase.getRange(sheetBase.getLastRow(), 10, 1, 3).setValues(source3);
  sheetBase.getRange(sheetBase.getLastRow(), 13, 1, 3).setValues(source4);

答案1

得分: 2

使用Array.concat()Array.flat()Sheet.appendRow(),像这样:

  const data = sourceEqpt.concat(origem.getRange('E12:G15').getValues()).flat();
  sheetBase.appendRow(data);
英文:

Use Array.concat(), Array.flat() and Sheet.appendRow(), like this:

  const data = sourceEqpt.concat(origem.getRange('E12:G15').getValues()).flat();
  sheetBase.appendRow(data);

huangapple
  • 本文由 发表于 2023年2月8日 22:20:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定