简体   繁体   English

Google Apps 脚本中 for Loop 的范围未找到错误

[英]Range not Found Error on for Loop in Google Apps Script

I'm looking to create templates using GAS -- all I need is to copy and paste variables into designated cells, push the data, then move to the next.我正在寻找使用 GAS 创建模板——我所需要的只是将变量复制并粘贴到指定的单元格中,推送数据,然后移动到下一个单元格。

I had my script working great for a single template (the first set of variables).我的脚本非常适用于单个模板(第一组变量)。 I tried to set up a loop, and now I'm hitting a "Range not found" error I'm going crazy trying to figure out.我试图建立一个循环,现在我遇到了一个“未找到范围”的错误,我正在疯狂地试图找出答案。

I had it working copying and pasting the variables out and generating the template, but struggling to get it to move to the next cell in each column.我让它复制并粘贴变量并生成模板,但努力让它移动到每列中的下一个单元格。

I made a range for each column where the script should loop through, meaning:我为脚本应该循环遍历的每一列设置了一个范围,意思是:

Row 2 Col A, Row 2 Col B, Row 2 Col C, Row 2 Col D each go through the loop (variables get pasted, sheet gets created, etc), then we move onto Row 3 Col A, Row 2 Col B, so on so forth. Row 2 Col A, Row 2 Col B, Row 2 Col C, Row 2 Col D each go through the loop (variables get pasted, sheet gets created, etc), then we move to Row 3 Col A, Row 2 Col B,依此类推。

function CreateTemplates() {
   var templatesNeeded = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');            
   var rundownSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Exclusive Template Creation');
   var tabname = templatesNeeded.getRange('A2').getValue();
   var pasteTemplate = rundownSheet.getRange('e1:l150').getValues();

    const row = 2;
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('Test');
  const rows = sheet.getLastRow() - row + 1;
  const range = sheet.getRange(row, 1, rows, 1);
  const values = range.getValues().flat();

 const dateRange = sheet.getRange(row, 2, rows, 1);
  const dateValues = dateRange.getValues().flat();


   const gameRange = sheet.getRange(row, 3, rows, 1);
  const gameValues = gameRange.getValues().flat();


   const typeRange = sheet.getRange(row, 4, rows, 1);
  const typeValues = typeRange.getValues().flat();

     const broadcastRange = sheet.getRange(row, 5, rows, 1);
  const broadcastValues = broadcastRange.getValues().flat();

  const matches = {};
  values.forEach((value, i) => {
    if (value !== '') {

  
 rundownSheet.getRange('E3').setValue(templatesNeeded.getRange(values).getValue());
   rundownSheet.getRange('B1').setValue(templatesNeeded.getRange(dateValues).getValue());
   rundownSheet.getRange('B2').setValue(templatesNeeded.getRange(gameValues).getValue());
   rundownSheet.getRange('B3').setValue(templatesNeeded.getRange(typeValues).getValue());
rundownSheet.getRange('H2').setValue(templatesNeeded.getRange(broadcastValues).getValue());

 
     var tss = SpreadsheetApp.openById('16nn9mfSOpVbPZBsB_nwhRNRv4nYQblrS1GzAD4jgmoQ');

 var sheet = tss.getSheetByName('Single Template');


 sheet.copyTo(tss).setName(tabname);


 
// Logger.log(value)

  sheet.getRange(1,1,pasteTemplate.length,pasteTemplate[0].length).setValues(pasteTemplate);



Utilities.sleep(1000);
    }
  });
}

I think it's clearly a problem with how I created my ranges (or something with the loop).我认为这显然是我创建范围(或循环的方式)的问题。 Any thoughts are greatly, greatly appreciated.任何想法都非常非常感谢。

Here's a test link where the code would be hosted:这是托管代码的测试链接:

https://docs.google.com/spreadsheets/d/1WeBgyi8JVfC02rCIB0T1eEbbmtpaXYF16qM7MLIzqPs/edit#gid=0 https://docs.google.com/spreadsheets/d/1WeBgyi8JVfC02rCIB0T1eEbbmtpaXYF16qM7MLIzqPs/edit#gid=0

Here's a test link where the code will be pushed to (templates will be created here):这是代码将被推送到的测试链接(将在此处创建模板):

https://docs.google.com/spreadsheets/d/16nn9mfSOpVbPZBsB_nwhRNRv4nYQblrS1GzAD4jgmoQ/edit#gid=804269175 https://docs.google.com/spreadsheets/d/16nn9mfSOpVbPZBsB_nwhRNRv4nYQblrS1GzAD4jgmoQ/edit#gid=804269175

Large improvement, but still hitting some errors.很大的改进,但仍然遇到一些错误。 Now, it's looping through and creating the tabs, but the setValues to actually move the data over doesn't seem to work.现在,它正在循环并创建选项卡,但实际移动数据的 setValues 似乎不起作用。 Thought it was a timing issue - I added in utilities.sleep but still playing with it, with no luck yet.认为这是一个时间问题 - 我添加了 utilities.sleep 但仍在玩它,还没有运气。 Basically, I want the variables to surface the data needed, that data to copy and paste to a template, then new variables, then new template:基本上,我希望变量显示所需的数据,将数据复制并粘贴到模板,然后是新变量,然后是新模板:

   function createNewTemplates() {
  const ss = SpreadsheetApp.getActive();
  const sh1 = ss.getSheetByName('Test');
  const sh2 = ss.getSheetByName('Exclusive Template Creation');
  const sh1A2 = sh1.getRange('A2').getValue();
  const vs2 = sh2.getRange('e1:l150').getValues();
  const sr = 2;
  // const sh1 = ss.getSheetByName('Test');
  const vs1 = sh1.getRange(sr, 1, sh1.getLastRow() - sr + 1, 5).getValues();
  const tss = SpreadsheetApp.openById('16nn9mfSOpVbPZBsB_nwhRNRv4nYQblrS1GzAD4jgmoQ');
  vs1.forEach((r, i) => {
    if (r[0] !== '') {
       Utilities.sleep(2000);
      sh2.getRange('E3').setValue(r[0]);
      sh2.getRange('B1').setValue(r[1]);
      sh2.getRange('B2').setValue(r[2]);
      sh2.getRange('B3').setValue(r[3]);
      sh2.getRange('H2').setValue(r[4]);
    
      let sh = tss.getSheetByName('Single Template');
      sh.copyTo(tss).setName(r[0]);
      let shnew = tss.getSheetByName(r[0]);
      Utilities.sleep(200);
      shnew.getRange(1, 1, vs2.length, vs2[0].length).setValues(vs2);
        
    }
  });
}

Try this:试试这个:

function createNewTemplates() {
  const ss = SpreadsheetApp.getActive();
  const sh1 = ss.getSheetByName('Test');
  const sh2 = ss.getSheetByName('Exclusive Template Creation');
  const vs2 = sh2.getRange('e1:l150').getValues();
  const sr = 2;
  const vs1 = sh1.getRange(sr, 1, sh1.getLastRow() - sr + 1, 5).getValues();
  const tss = SpreadsheetApp.openById('16nn9mfSOpVbPZBsB_nwhRNRv4nYQblrS1GzAD4jgmoQ');
  let sh = tss.getSheetByName('Single Template');
  vs1.forEach((r, i) => {
    if (r[0] !== '') {
      sh2.getRange('E3').setValue(r[0]);
      sh2.getRange('B1').setValue(r[1]);
      sh2.getRange('B2').setValue(r[2]);
      sh2.getRange('B3').setValue(r[3]);
      sh2.getRange('H2').setValue(r[4]);
      let shnew = sh.copyTo(tss).setName(r[0]);
      shnew.getRange(1, 1, vs2.length, vs2[0].length).setValues(vs2);
    }
  });
}

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

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