Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 将公式设置为多个单元格脚本-必须有更有效的方法_Google Apps Script_Google Sheets_Google Sheets Formula - Fatal编程技术网

Google apps script 将公式设置为多个单元格脚本-必须有更有效的方法

Google apps script 将公式设置为多个单元格脚本-必须有更有效的方法,google-apps-script,google-sheets,google-sheets-formula,Google Apps Script,Google Sheets,Google Sheets Formula,在GoogleSheets中,我想将以下公式应用于A2、A14、A26和A28,并将另一个公式应用于B2、B14、B26和B28。现在我只做了以下四次。它很管用,但并不漂亮。我怎样才能更有效地做到这一点 function onEdit(e) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheets = ss.getSheets(); sheets[7].setName('.NCF'); var sheet =

在GoogleSheets中,我想将以下公式应用于A2、A14、A26和A28,并将另一个公式应用于B2、B14、B26和B28。现在我只做了以下四次。它很管用,但并不漂亮。我怎样才能更有效地做到这一点

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  sheets[7].setName('.NCF');
  
  var sheet = ss.getSheets()[5];
  var cell1a = sheet.getRange("A2");
    cell1a.setFormula("=iferror(IF(QUERY('.NCF'!A2, \"where A contains '1ST'\")<>\"\", 1, ),\"\")");
  var cell1b = sheet.getRange("A14");
    cell1a.setFormula("=iferror(IF(QUERY('.NCF'!A14, \"where A contains '1ST'\")<>\"\", 1, ),\"\")");
  var cell1c = sheet.getRange("A26");
    cell1c.setFormula("=iferror(IF(QUERY('.NCF'!A26, \"where A contains '1ST'\")<>\"\", 1, ),\"\")");
  var cell1d = sheet.getRange("A38");
    cell1d.setFormula("=iferror(IF(QUERY('.NCF'!A38, \"where A contains '1ST'\")<>\"\", 1, ),\"\")");
    
  var cell2a = sheet.getRange("B2");
    cell2a.setFormula("=iferror(if(A2=1,query('.NCF'!A:A,\"Select A where A contains '( G54.1P'\"),\"\"),\"\")");
  var cell2b = sheet.getRange("B14");
    cell2a.setFormula("=iferror(if(A14=2,query('.NCF'!A:A,\"Select A where A contains '( G54.1P'\"),\"\"),\"\")");
  var cell2c = sheet.getRange("B26");
    cell2a.setFormula("=iferror(if(A26=3,query('.NCF'!A:A,\"Select A where A contains '( G54.1P'\"),\"\"),\"\")");
  var cell2d = sheet.getRange("B38");
    cell2a.setFormula("=iferror(if(A38=4,query('.NCF'!A:A,\"Select A where A contains '( G54.1P'\"),\"\"),\"\")");
}
函数onEdit(e){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheets=ss.getSheets();
表[7]。集合名('.NCF');
var sheet=ss.getSheets()[5];
var cell1a=表1.getRange(“A2”);
cell1a.setFormula(“=iferror(IF(查询('.NCF'!A2,\”,其中A包含'1ST'\”)\”,“\”,1,),“\”));
var cell1b=sheet.getRange(“A14”);
cell1a.setFormula(“=iferror(如果(查询('.NCF'!A14,\”,其中A包含'1ST'\”)\”,“\”,1,),“\”));
var cell1c=表。getRange(“A26”);
cell1c.setFormula(“=iferror(IF(查询('.NCF'!A26,\”,其中A包含'1ST'\”)\”,“\”,1,),“\”));
var cell1d=sheet.getRange(“A38”);
cell1d.setFormula(“=iferror(如果(查询('.NCF'!A38,\”,其中A包含'1ST'\”)\”\”,1,,\”),\”));
var cell2a=表。getRange(“B2”);
cell2a.setFormula(“=iferror(如果(A2=1,查询('.NCF'!A:A,\”选择A,其中A包含“(G54.1P'\”,\”,\”),\”));
var cell2b=sheet.getRange(“B14”);
cell2a.setFormula(“=iferror(如果(A14=2,查询('.NCF'!A:A,\”选择A中包含的A'”(G54.1P'\”,\“\”,\”));
var cell2c=sheet.getRange(“B26”);
cell2a.setFormula(“=iferror(如果(A26=3,查询('.NCF'!A:A,\”选择A中包含的A'”(G54.1P'\”,\“\”,\”));
var cell2d=sheet.getRange(“B38”);
cell2a.setFormula(“=iferror(如果(A38=4,查询('.NCF'!A:A,\”选择A中包含的A'”(G54.1P'\”,\“\”,\”));
}
  • 用于覆盖行号和列字母

  • 用于创建要设置的公式

函数onEdit(e){
常数ss=e.source;
const sheets=ss.getSheets();
表[7]。集合名('.NCF');
常数表=表[5];
让a1;
['A','B'].forEach(列=>
[2,14,26,38]。forEach((行,i)=>
床单
.getRange((a1=列+行))
.setFormula(
列=='A'
?`=IFERROR(IF(查询('.NCF'!${a1},“其中A包含'1ST'”),1,)`
:`=IFERROR(如果(A${row}=${i+
1} ,查询('.NCF'!A:A,“选择A包含的位置”(G54.1P'))`
)
)
);
}
另请参见。

  • 用于覆盖行号和列字母

  • 用于创建要设置的公式

函数onEdit(e){
常数ss=e.source;
const sheets=ss.getSheets();
表[7]。集合名('.NCF');
常数表=表[5];
让a1;
['A','B'].forEach(列=>
[2,14,26,38]。forEach((行,i)=>
床单
.getRange((a1=列+行))
.setFormula(
列=='A'
?`=IFERROR(IF(查询('.NCF'!${a1},“其中A包含'1ST'”),1,)`
:`=IFERROR(如果(A${row}=${i+
1} ,查询('.NCF'!A:A,“选择A包含的位置”(G54.1P'))`
)
)
);
}
另请参见。

解决方案: 这是我的解决方案,它基于与主答案相同的逻辑

  function onEdit(e) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheets = ss.getSheets();
  sheets[7].setName('.NCF');
  const sheet = ss.getSheets()[5];
  const cells1 = ['A2','A14','A26','A38'];
  const cells2 = ['B2','B14','B26','B38'];
  cells1.forEach(c=>          
     sheet.getRange(c).setFormula(`iferror(IF(QUERY('.NCF'!${c}, \"where A contains '1ST'\")<>\"\", 1, ),\"\")`)
  )                                                
  cells2.forEach((c,i)=>          
     sheet.getRange(c).setFormula(`iferror(if(${cells1[i]}=${i+1},query('.NCF'!A:A,\"Select A where A contains '( G54.1P'\"),\"\"),\"\")`)
  )   
}
函数onEdit(e){
const ss=SpreadsheetApp.getActiveSpreadsheet();
const sheets=ss.getSheets();
表[7]。集合名('.NCF');
const sheet=ss.getSheets()[5];
常量单元格1=['A2'、'A14'、'A26'、'A38'];
常量单元格2=['B2'、'B14'、'B26'、'B38'];
单元格1.forEach(c=>
sheet.getRange(c).setFormula(`iferror(IF(查询('.NCF'!${c},\“其中A包含'1ST'\”)\“\”,1,),\“\”)
)                                                
单元格2.forEach((c,i)=>
sheet.getRange(c).setFormula(`iferror(如果(${cells1[i]}=${i+1}),查询('.NCF'!A:A,\'选择A中包含的A'(G54.1P'\”,\“\”,\“\”,\“\”)
)   
}
说明: 显然,您需要使用两个单元格序列和两个公式。这就是我使用
cells1
cells2
分别表示要应用公式1和公式2的单元格集的原因。您需要调用此表达式:
sheet.getRange().setFormula
用于每个单元格,这就是为什么可以使用
forEach
函数的原因。最后,我使用将
单元格
表的元素合并到字符串参数中

我的解决方案与其他解决方案的主要区别在于,我的语句彼此之间没有链接,并且我不使用if语句。

解决方案: 这是我的解决方案,它基于与主答案相同的逻辑

  function onEdit(e) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheets = ss.getSheets();
  sheets[7].setName('.NCF');
  const sheet = ss.getSheets()[5];
  const cells1 = ['A2','A14','A26','A38'];
  const cells2 = ['B2','B14','B26','B38'];
  cells1.forEach(c=>          
     sheet.getRange(c).setFormula(`iferror(IF(QUERY('.NCF'!${c}, \"where A contains '1ST'\")<>\"\", 1, ),\"\")`)
  )                                                
  cells2.forEach((c,i)=>          
     sheet.getRange(c).setFormula(`iferror(if(${cells1[i]}=${i+1},query('.NCF'!A:A,\"Select A where A contains '( G54.1P'\"),\"\"),\"\")`)
  )   
}
函数onEdit(e){
const ss=SpreadsheetApp.getActiveSpreadsheet();
const sheets=ss.getSheets();
表[7]。集合名('.NCF');
const sheet=ss.getSheets()[5];
常量单元格1=['A2'、'A14'、'A26'、'A38'];
常量单元格2=['B2'、'B14'、'B26'、'B38'];
单元格1.forEach(c=>
sheet.getRange(c).setFormula(`iferror(IF(查询('.NCF'!${c},\“其中A包含'1ST'\”)\“\”,1,),\“\”)
)                                                
单元格2.forEach((c,i)=>
sheet.getRange(c).setFormula(`iferror(如果(${cells1[i]}=${i+1}),查询('.NCF'!A:A,\'选择A中包含的A'(G54.1P'\”,\“\”,\“\”,\“\”)
)   
}
说明: 显然,您需要使用两个单元格序列和两个公式。这就是我使用
cells1
cells2
分别表示要应用公式1和公式2的单元格集的原因。您需要调用此表达式:
sheet.getRange().setFormula
用于每个单元格,这就是为什么可以使用
forEach
函数的原因。最后,我使用将
单元格
表的元素合并到字符串参数中


我的解决方案与其他解决方案之间的主要区别在于,我的语句彼此之间没有链接,并且我不使用if语句。

或者,您可以使用批处理来应用值,以获得更好的性能。如果您在值前面加上
=
,这是必要的
function onEdit(e) {

  const ss = SpreadsheetApp.getActiveSpreadsheet();

  const sheets = ss.getSheets();

  sheets[7].setName('.NCF');

  const [, , , , , sheet] = ss.getSheets();

  const rng = sheet.getDataRange();
  const vals = rng.getValues();
  const formulas = rng.getFormulas();

  const mixed = formulas.map((r, ri) => r.map((c, ci) => c || vals[ri][ci]));

  const toFormula = [2, 14, 26, 38].map(r => r - 1);
  let formulaIdx = 1;

  const newVals = mixed.map((row, ri) => {
    if (!toFormula.includes(ri)) {
      return row;
    }
    row[0] = `=iferror(IF(QUERY('.NCF'!A${ri+1}, \"where A contains '1ST'\")<>\"\", 1, ),\"\")`;
    row[1] = `iferror(if(A${ri + 1}=${formulaIdx++},query('.NCF'!A:A,\"Select A where A contains '( G54.1P'\"),\"\"),\"\")`;
    return row;
  });

  rng.setValues(newVals);

  console.log(rng.getValues());
}