Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.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
Javascript Google Sheets脚本复制每个国家/地区代码的数据_Javascript_Google Sheets - Fatal编程技术网

Javascript Google Sheets脚本复制每个国家/地区代码的数据

Javascript Google Sheets脚本复制每个国家/地区代码的数据,javascript,google-sheets,Javascript,Google Sheets,我有一个产品列表及其属性。列名示例如下: id、可用性、条件、描述、标题、价格、销售价格 在另一张“作弊”表中,我列出了国家代码,如FR和DE 对于每个国家/地区代码,我希望复制产品数据,对于每个国家/地区代码,在和“覆盖”命名列中添加国家/地区代码 以下是指向示例文档的链接: 感谢您提供的任何帮助,因为我正在努力入门。我检查了您的示例文档表,并提出了以下方法: 建议 我已经创建了一个名为showResult()的自定义函数脚本: 函数showResult(){ var ss=Spreadsh

我有一个产品列表及其属性。列名示例如下:

id、可用性、条件、描述、标题、价格、销售价格

在另一张“作弊”表中,我列出了国家代码,如FR和DE

对于每个国家/地区代码,我希望复制产品数据,对于每个国家/地区代码,在和“覆盖”命名列中添加国家/地区代码

以下是指向示例文档的链接:


感谢您提供的任何帮助,因为我正在努力入门。

我检查了您的示例文档表,并提出了以下方法:

建议

我已经创建了一个名为
showResult()
的自定义函数脚本:

函数showResult(){
var ss=SpreadsheetApp.getActive();
var products=ss.getSheetByName('products');//获取'products'工作表上的所有数据
var cheat=ss.getSheetByName('cheat');//获取“cheat”工作表上的所有数据
var row=products.getDataRange().getNumRows();//统计“products”工作表上当前的行数
var cheatRow=cheat.getDataRange().getNumRows();//统计“备忘”表上当前的行数
var finalResult=[[products.getRange(1,1).getValue().toString(),//首先在finalResult数组上初始化列标题
products.getRange(1,2).getValue().toString(),
products.getRange(1,3).getValue().toString(),
products.getRange(1,4).getValue().toString(),
products.getRange(1,5).getValue().toString(),
'override']];//添加'override'列

对于(var i=2;我感谢你,只是尝试一下这个,但是达到了整个工作表的最大执行时间。我已经搜索了这个,并将尝试与你的脚本交互。非常感谢。
function showResult() {
  var ss = SpreadsheetApp.getActive();
  var products = ss.getSheetByName('products'); //Gets all data on 'products' sheet
  var cheat = ss.getSheetByName('cheat'); //Gets all data on 'cheat' sheet
  var row = products.getDataRange().getNumRows(); //Counts current # of rows on 'products' sheet
  var cheatRow = cheat.getDataRange().getNumRows(); //Counts current # of rows on 'cheat' sheet
  var finalResult = [[products.getRange(1,1).getValue().toString(), //Initalize the column titles first on the finalResult array
                  products.getRange(1,2).getValue().toString(),
                  products.getRange(1,3).getValue().toString(),
                  products.getRange(1,4).getValue().toString(),
                  products.getRange(1,5).getValue().toString(),
                  'override']]; //Adds the 'override' column
  for(var i=2; i<=cheatRow; i++){ //First loop to get each country codes
      for(var x=2; x<=row; x++){ //Final loop to add each country codes to the each copy of grouped product values
      finalResult.push([products.getRange(x,1).getValue().toString(),
                    products.getRange(x,2).getValue().toString(),
                    products.getRange(x,3).getValue().toString(),
                    products.getRange(x,4).getValue().toString(),
                    products.getRange(x,5).getValue().toString(),
                    cheat.getRange(i,1).getValue()
                    ]);
    }
  }
  SpreadsheetApp.getActive().getSheetByName('output').clear().getRange(1, 1, finalResult.length, finalResult[0].length).setValues(finalResult);
}

function onOpen() { //[Optional] Added a custom menu to manually to refresh your 'output' sheet
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Refresh output sheet')
      .addItem('Refresh', 'showResult')
      .addToUi();
}