Javascript Google脚本作为两个单独的脚本工作,但不在同一个函数中

Javascript Google脚本作为两个单独的脚本工作,但不在同一个函数中,javascript,google-apps-script,data-structures,formatting,Javascript,Google Apps Script,Data Structures,Formatting,基本上,我有一个分为4个块的脚本: 1.在一个范围内复制每一行,前提是它满足一个条件 2.删除所有空行 3.将所有数字设置为百分比 4.将条件单元格格式应用于其中一列 第四部分是引起我问题的部分。脚本运行时没有任何错误消息,如果块4单独在另一个脚本中,并且定义了相同的变量,那么它就可以很好地工作,但是只要它与其他脚本在同一个函数中,它就不会在没有任何类型的错误消息的情况下运行 尝试将变量的名称更改为单用变量,以确保不是因为在其上方修改了一个“var”,删除了“else if”以在循环中仅保留一个

基本上,我有一个分为4个块的脚本: 1.在一个范围内复制每一行,前提是它满足一个条件 2.删除所有空行 3.将所有数字设置为百分比 4.将条件单元格格式应用于其中一列

第四部分是引起我问题的部分。脚本运行时没有任何错误消息,如果块4单独在另一个脚本中,并且定义了相同的变量,那么它就可以很好地工作,但是只要它与其他脚本在同一个函数中,它就不会在没有任何类型的错误消息的情况下运行

尝试将变量的名称更改为单用变量,以确保不是因为在其上方修改了一个“var”,删除了“else if”以在循环中仅保留一个“if”,将其移动到脚本的其他部分,但如果块1在脚本中,则块4将不适用(如果仅与2和3一起,则将应用)

遵循相同结构的2和3与1配合良好

有人知道我的剧本有什么问题吗?:) 每个块都用它的功能进行注释

function copy() {
//Set variables & criterion to choose which rows to copy
  var s = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1bEiLWsbFszcsz0tlQudMBgTk5uviyv_wDx7fFa8txFM/edit');
  var ssSource = s.getSheetByName('Variations');
  var ssDest = s.getSheetByName('Email');
  var lastRowSource = ssSource.getLastRow();
  var lastRowDest = ssDest.getLastRow();
  var lastColSource = ssSource.getLastColumn()
  var criteria = 0;
  var titles = ssSource.getRange(1,1,1, lastColSource).getValues()


//Copies the range
  ssDest.getRange(1,1,1, lastColSource).setValues(titles)

  for (var i = 2; i < lastRowSource; i++ ) {
    var test = ssSource.getRange(i ,1);    
    Logger.log(test.getValue()+ ' <? ' + criteria);
    if (ssSource.getRange(i ,6).getValue() > criteria) { 

      ssSource.getRange(i ,1,1,ssSource.getLastColumn()).copyTo(ssDest.getRange(i ,1,1,ssSource.getLastColumn()), {contentsOnly:true}); // copy/paste content only
}  
    }

//Removes empty rows
  var data = ssDest.getDataRange().getValues();
  var targetData = new Array();
  for(n=0;n<data.length;++n){
    if(data[n].join().replace(/,/g,'')!=''){ targetData.push(data[n])};
    Logger.log(data[n].join().replace(/,/g,''))
  }
  ssDest.getDataRange().clear();
  ssDest.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);

//Formats numbers as percentages
  var rangePercent = ssDest.getRange(1,1,ssDest.getLastRow(),ssDest.getLastColumn());
  var rowsPercent = rangePercent.getNumRows();
  var colsPercent = rangePercent.getNumColumns();

  for(var rowPercent = 1; rowPercent <= rowsPercent; rowPercent++) {
    for(var colPercent = 1; colPercent <= colsPercent; colPercent++) {
      var cellPercent = rangePercent.getCell(rowPercent, colPercent);
      var valuePercent = cellPercent.getValue();
      if(typeof(valuePercent) == 'number') {
        cellPercent.setNumberFormat("##.#%");
      }
    }
  }

//Adds conditional background colours

  for (var z = 2; z < lastRowDest+1;z++) {
    var avgCpc = 4;
    var rangeColour = ssDest.getRange(z,avgCpc);
    var dataColour = rangeColour.getValue()
    if (dataColour < 0) {
      ssDest.getRange(z,avgCpc).setBackground('#d9ead3')
      }
            else if (dataColour > 0) {
        ssDest.getRange(z,avgCpc).setBackground('#f4cccc')
      }

  }

//Centers Values

}
函数复制(){
//设置变量和条件以选择要复制的行
var s=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1bEiLWsbFszcsz0tlQudMBgTk5uviyv_wDx7fFa8txFM/edit');
var ssSource=s.getSheetByName(“变体”);
var ssDest=s.getSheetByName(“电子邮件”);
var lastRowSource=ssSource.getLastRow();
var lastRowDest=ssDest.getLastRow();
var lastColSource=ssSource.getLastColumn()
var标准=0;
var titles=ssSource.getRange(1,1,1,lastColSource).getValues()
//复制范围
ssDest.getRange(1,1,1,lastColSource).setValue(标题)
对于(var i=2;iLogger.log(test.getValue()+”您遇到的问题是您的代码存在性能问题,因为您调用了太多的方法,例如各种循环中的和,因此应用程序脚本无法跟上所有这些调用。请检查

话虽如此,我修改了您的代码以提高效率。除了您的
copy
函数外,我还添加了两个函数以提高代码的可读性

功能副本 与前面一样,此函数设置变量,但现在它调用了另外两个函数,即
setPositiveCostValues
formatCells

function copy() {
  //Set variables & criterion to choose which rows to copy
  var ss = SpreadsheetApp.openByUrl('your-url');
  var ssSource = ss.getSheetByName('Variations');
  var ssDest = ss.getSheetByName('Email');
  // set the title
  var titles = ssSource.getRange(1,1,1, ssSource.getLastColumn()).getValues();
  ssDest.getRange(1,1,1, ssSource.getLastColumn()).setValues(titles);
  // get the positive values you want from the cost col
  var positiveValues = setPositiveCostValues(ssSource, ssDest, ssSource.getLastRow());
  // fomrat the cells you want as percentage and set the color
  formatCells(ssDest, positiveValues);
}
函数setPositiveCostValues 这将取成本为正值的值,并将从具有空值和“n/a”值的单元格中提取

功能格式化单元 这将按百分比格式化成本列中的单元格,并在avgCpc列中设置正确的颜色

function formatCells(ssDest, postiveCost){
  var avgCpc = 4, cost = 6, row = 2, criteria = 0;
  // iterate over the array and depending on the criteria format the cells 
  postiveCost.forEach(function(el){
    if(el[cost - 1] > criteria){
      var ssDestRange = ssDest.getRange(row, 1, 1, cost);
      ssDestRange.setValues([el]);
      ssDestRange.getCell(1, cost).setNumberFormat("##.#%");
      // set the color depending on the avgCpc value condition
      if(el[avgCpc - 1] < criteria) ssDest.getRange(row, avgCpc).setBackground('#d9ead3');
      else ssDest.getRange(row, avgCpc).setBackground('#f4cccc');
      row++;
    }
  });
}
函数格式单元格(ssDest、postiveCost){
var avgCpc=4,成本=6,行=2,标准=0;
//迭代数组并根据条件设置单元格的格式
postiveCost.forEach(功能(el){
如果(el[成本-1]>标准){
var ssDestRange=ssDest.getRange(行,1,1,成本);
ssDestRange.setValues([el]);
ssDestRange.getCell(1,cost).setNumberFormat(“##.#%”);
//根据avgCpc值条件设置颜色
如果(el[avgCpc-1]<标准)ssDest.getRange(世界其他地区,avgCpc).setBackground('d9ead3');
else ssDest.getRange(世界其他地区,平均GCPC).setBackground('f4cccc');
行++;
}
});
}
代码 现在,您的整个代码将如下所示:

function copy() {
  //Set variables & criterion to choose which rows to copy
  var ss = SpreadsheetApp.openByUrl('your-url');
  var ssSource = ss.getSheetByName('Variations');
  var ssDest = ss.getSheetByName('Email');
  // set the title
  var titles = ssSource.getRange(1,1,1, ssSource.getLastColumn()).getValues();
  ssDest.getRange(1,1,1, ssSource.getLastColumn()).setValues(titles);
  // get the positive values you want from the cost col
  var positiveValues = setPositiveCostValues(ssSource, ssDest, ssSource.getLastRow());
  // fomrat the cells you want as percentage and set the color
  formatCells(ssDest, positiveValues);
}

function setPositiveCostValues(ssSource,ssDest, lastRowSource){
  var postiveCost = ssSource.getRange(2, 1, lastRowSource, 6).getValues();
  // this loop will clean the empty elements and the ones that only have n/a
  for (var i = postiveCost.length - 1; i >= 0; i--) {
    if (postiveCost[i][0]) {
      postiveCost.splice(i + 1, postiveCost.length - (i + 1));
      postiveCost = postiveCost.filter(function(el){ return el != 'n/a'})
      break;
    }
  }
  return postiveCost;
}

function formatCells(ssDest, postiveCost){
  var avgCpc = 4, cost = 6, row = 2, criteria = 0;
  // iterate over the array and depending on the criteria format the cells 
  postiveCost.forEach(function(el){
    if(el[cost - 1] > criteria){
      var ssDestRange = ssDest.getRange(row, 1, 1, cost);
      ssDestRange.setValues([el]);
      ssDestRange.getCell(1, cost).setNumberFormat("##.#%");
      // set the color depending on the avgCpc value condition
      if(el[avgCpc - 1] < criteria) ssDest.getRange(row, avgCpc).setBackground('#d9ead3');
      else ssDest.getRange(row, avgCpc).setBackground('#f4cccc');
      row++;
    }
  });
}
函数复制(){
//设置变量和条件以选择要复制的行
var ss=SpreadsheetApp.openByUrl('your-url');
var ssSource=ss.getSheetByName(“变体”);
var ssDest=ss.getSheetByName('Email');
//设置标题
var titles=ssSource.getRange(1,1,1,ssSource.getLastColumn()).getValues();
ssDest.getRange(1,1,1,ssSource.getLastColumn()).setValue(标题);
//从成本列中获取所需的正值
var positiveValues=setPositiveCostValues(ssSource、ssDest、ssSource.getLastRow());
//fomrat您想要的单元格的百分比,并设置颜色
formatCells(ssDest,阳性值);
}
函数setPositiveCostValues(ssSource、ssDest、lastRowSource){
var postiveCost=ssSource.getRange(2,1,lastRowSource,6).getValues();
//此循环将清除空元素和只有n/a的元素
对于(变量i=postiveCost.length-1;i>=0;i--){
if(postiveCost[i][0]){
postiveCost.拼接(i+1,postiveCost.length-(i+1));
postiveCost=postiveCost.filter(函数(el){return el!='n/a'})
打破
}
}
返回postiveCost;
}
函数formatCells(ssDest、postiveCost){
var avgCpc=4,成本=6,行=2,标准=0;
//迭代数组并根据条件设置单元格的格式
postiveCost.forEach(功能(el){
如果(el[成本-1]>标准){
var ssDestRange=ssDest.getRange(行,1,1,成本);
ssDestRange.setValues([el]);
ssDestRange.getCell(1,cost).setNumberFormat(“##.#%”);
//根据avgCpc值条件设置颜色
如果(el[avgCpc-1]<标准)ssDest.getRange(世界其他地区,avgCpc).setBackground('d9ead3');
else ssDest.getRange(世界其他地区,平均GCPC).setBackground('f4cccc');
行++;
}
});
}

尝试插入SpreadsheetApp.flush()在第三区之后。你能提供一份包含一些虚拟数据的电子表格吗?这将更容易找到你问题的原因。嗨@Cooper这不起作用unfoHi@albertovielma我的公司在数据共享方面非常严格,因此我的访问级别阻止我共享任何电子邮件地址未注册为公司的文件…嗯,那么我想我帮不了你。我为浪费你的时间道歉。非常感谢你@alberto-以我有限的知识,我永远不会找到解决办法:)
function copy() {
  //Set variables & criterion to choose which rows to copy
  var ss = SpreadsheetApp.openByUrl('your-url');
  var ssSource = ss.getSheetByName('Variations');
  var ssDest = ss.getSheetByName('Email');
  // set the title
  var titles = ssSource.getRange(1,1,1, ssSource.getLastColumn()).getValues();
  ssDest.getRange(1,1,1, ssSource.getLastColumn()).setValues(titles);
  // get the positive values you want from the cost col
  var positiveValues = setPositiveCostValues(ssSource, ssDest, ssSource.getLastRow());
  // fomrat the cells you want as percentage and set the color
  formatCells(ssDest, positiveValues);
}

function setPositiveCostValues(ssSource,ssDest, lastRowSource){
  var postiveCost = ssSource.getRange(2, 1, lastRowSource, 6).getValues();
  // this loop will clean the empty elements and the ones that only have n/a
  for (var i = postiveCost.length - 1; i >= 0; i--) {
    if (postiveCost[i][0]) {
      postiveCost.splice(i + 1, postiveCost.length - (i + 1));
      postiveCost = postiveCost.filter(function(el){ return el != 'n/a'})
      break;
    }
  }
  return postiveCost;
}

function formatCells(ssDest, postiveCost){
  var avgCpc = 4, cost = 6, row = 2, criteria = 0;
  // iterate over the array and depending on the criteria format the cells 
  postiveCost.forEach(function(el){
    if(el[cost - 1] > criteria){
      var ssDestRange = ssDest.getRange(row, 1, 1, cost);
      ssDestRange.setValues([el]);
      ssDestRange.getCell(1, cost).setNumberFormat("##.#%");
      // set the color depending on the avgCpc value condition
      if(el[avgCpc - 1] < criteria) ssDest.getRange(row, avgCpc).setBackground('#d9ead3');
      else ssDest.getRange(row, avgCpc).setBackground('#f4cccc');
      row++;
    }
  });
}