我的代码中有两个错误

时间:2015-09-27 09:47:06

标签: google-sheets

我想将活动行复制并粘贴到另一张表中,但它给了我两个错误:

  1. 它无法识别我的输入值。
  2. 它没有粘贴任何数据,并且未找到copyTo

    function Anunaki() {  
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheetOne = ss.getSheetByName("KEY WORK");
        var sheetTwo = ss.getSheetByName("BUGS");
        var j = sheetTwo.getLastRow() + 1;
        var data = sheetOne.getActiveRange().getValues();
        var columnCount = data[0].length
        var ui = SpreadsheetApp.getUi();
    
        var response = ui.prompt('BUG Confirmation', 'How many Interactions ?', ui.ButtonSet.YES_NO);
    
        if (response === "0")   
        {
            ui.alert('Opps Zero I don\'t believe you, Try again');
        }
        else if (response === "1")
        {
            sheetOne.getRange(1,1,1,columnCount).copyTo(sheetTwo.getRange(j,1,1,columnCount));
        }
        else if (response === "2" ) 
        {
            sheetOne.getRange(1,1,1,columnCount).copyTo(sheetTwo.getRange(j,1,1,columnCount));
        }
        else 
        {
            ui.alert('Please enter a numeric value between 1 to 2');
        }
    }
    

1 个答案:

答案 0 :(得分:0)

替换代码中的以下行:

var response = ui.prompt('BUG Confirmation', 'How many Interactions ?', ui.ButtonSet.YES_NO);

要:

var response = ui.prompt('BUG Confirmation', 'How many Interactions ?', ui.ButtonSet.YES_NO).getResponseText();

===============
编辑: 以下是应该有效的完整代码:

  function Anunaki() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetOne = ss.getSheetByName("KEY WORK");
  var sheetTwo = ss.getSheetByName("BUGS");

  if( ss.getActiveSheet().getName() != sheetOne.getName() )
  return;

  var j = sheetTwo.getLastRow() + 1;
  var data = sheetOne.getDataRange().getValues();
  var row = sheetOne.getActiveRange().getRow();
  var columnCount = data[0].length
  var ui = SpreadsheetApp.getUi();

  var response = ui.prompt('BUG Confirmation', 'How many Interactions ?', ui.ButtonSet.YES_NO).getResponseText();

  if (response === "0") {
    ui.alert('Opps Zero I don\'t believe you, Try again');
  }

  else if (response === "1") {
    sheetOne.getRange(row,1,1,columnCount).copyTo(sheetTwo.getRange(j,1,1,columnCount));
  }

  else if (response === "2" ) {
    sheetOne.getRange(row,1,1,columnCount).copyTo(sheetTwo.getRange(j,1,1,columnCount));
  }
  else {
    ui.alert('Please enter a numeric value between 1 to 2');
  }

};

注意:
我添加了' var row'在脚本中获取需要在getRange函数中使用的当前行以获取整个行范围的值。



修改:要仅获取目标表中的值而不是公式,请尝试以下代码:

function Anunaki() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetOne = ss.getSheetByName("KEY WORK");
  var sheetTwo = ss.getSheetByName("BUGS");

  if( ss.getActiveSheet().getName() != sheetOne.getName() )
  return;

  var j = sheetTwo.getLastRow() + 1;
  var data = sheetOne.getDataRange().getValues();
  var row = sheetOne.getActiveRange().getRow();
  var columnCount = data[0].length
  var ui = SpreadsheetApp.getUi();

  var response = ui.prompt('BUG Confirmation', 'How many Interactions ?', ui.ButtonSet.YES_NO).getResponseText();

  if (response === "0") {
    ui.alert('Opps Zero I don\'t believe you, Try again');
  }

  else if (response === "1") {
    var values = sheetOne.getRange(row,1,1,columnCount).getValues();
    sheetTwo.getRange(j,1,1,columnCount).setValues(values)
  }

  else if (response === "2" ) {
    var values = sheetOne.getRange(row,1,1,columnCount).getValues();
    sheetTwo.getRange(j,1,1,columnCount).setValues(values)
  }
  else {
    ui.alert('Please enter a numeric value between 1 to 2');
  }

};