将一行Google表格电子表格中的一行复制到表格提交上的另一行

时间:2017-06-27 19:36:09

标签: javascript forms google-apps-script google-sheets

我一直在阅读其他一些帖子并尝试了一些事情,但我还没有找到解决方案。

我有一个Google表单,可以输入电子表格。当有人提交时,我想根据N(14)列中的数据确定新行将被复制到何处。如果是A,则将整行复制到电子表格A,如果是B,则将整行复制到电子表格B.

如果有人需要玩这些网址,网址就是原件的副本。表格在此处:Copy of New Signage Form

function movingRows() {

var spreadsheetUrlToWatch = 'https://docs.google.com/spreadsheets/d/1q7HyUL3bhd_gLEAfVJrH4strnkF9_UP6uhY4Qy5A6Vc/edit#gid=291411519';

var columnNumberToWatch = 14; // column A = 1, B = 2, etc.
var valueToWatchA = "Fusion";
var valueToWatchB = "LightSource";
var spreadsheetUrlToMoveTheRowToA = "https://docs.google.com/spreadsheets/d/1sWSSHOgdum0LtCggtM6Gz-ytmFJJjMXH9pZ0NYGMV7g/edit#gid=0";
var spreadsheetUrlToMoveTheRowToB = "https://docs.google.com/spreadsheets/d/1BM7x-5STx5DsEtzRiZEq8qxVXgjPjtIZoTxxWuAk46c/edit#gid=0";

var ss = SpreadsheetApp.openByUrl(spreadsheetUrlToWatch);
var sheet = ss.getSheets()[0];
var range = sheet.getActiveCell();
var sheetNameToWatch = ss.getSheets()[0].getSheetName();
console.log(sheetNameToWatch);

if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatchA) {

  var targetSheet = SpreadsheetApp.openByUrl(spreadsheetUrlToMoveTheRowToA).getSheets()[0];
  var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
  sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).copyTo(targetRange);
}
else if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatchB) {
  var targetSheet = SpreadsheetApp.openByUrl(spreadsheetUrlToMoveTheRowToB).getSheets()[0];
  var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
  sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).copyTo(targetRange);
}

}

代码通过谷歌脚本编辑器附加到表单。在Cooper提供的解决方案之后使用的代码:

function copyToSpreadsheet()
 {
   var ss0=SpreadsheetApp.openById('1q7HyUL3bhd_gLEAfVJrH4strnkF9_UP6uhY4Qy5A6Vc');
   var sht0=ss0.getSheetByName('Form Responses 1');
   var rng0=sht0.getDataRange();
   var rng0A=rng0.getValues();
   //Fusion Spreadsheet
   var ssA=SpreadsheetApp.openById('1sWSSHOgdum0LtCggtM6Gz-ytmFJJjMXH9pZ0NYGMV7g');
   var shtA=ssA.getSheetByName('Sheet1');
   //LightSource Spreadsheet
   var ssB=SpreadsheetApp.openById('1BM7x-5STx5DsEtzRiZEq8qxVXgjPjtIZoTxxWuAk46c');
   var shtB=ssB.getSheetByName('Sheet1');
   var row= rng0A[rng0A.length-1];
   if(row[13]== "Fusion")
   {
     shtA.appendRow(row);
   }
   if(row[13]== "LightSource")
   {
    shtB.appendRow(row);
   }
  }

1 个答案:

答案 0 :(得分:2)

我没有测试这段代码,但我认为它很接近。这很简单。只需将其绑定到onFormSubmit,它就会获得最后一个条目。我假设已经创建了所有电子表格,并且他们有一个Sheet1。

function copyToSpreadsheet()
{
  var ss0=SpreadsheetApp.openById('1q7HyUL3bhd_gLEAfVJrH4strnkF9_UP6uhY4Qy5A6Vc');
  var sht0=ss0.getSheetByName('Sheet1');
  var rng0=sht0.getDataRange();
  var rng0A=rng0.getValues();
  var ssA=SpreadsheetApp.openById('1sWSSHOgdum0LtCggtM6Gz-ytmFJJjMXH9pZ0NYGMV7g');
  var shtA=ssA.getSheetByName('Sheet1');
  var ssB=SpreadsheetApp.openById('1sWSSHOgdum0LtCggtM6Gz-ytmFJJjMXH9pZ0NYGMV7g');
  var shtB=ssB.getSheetByName('Sheet1');
  var row= rng0A[rng0A.length-1];
  if(row[13]==1)
  {
    shtA.appendRow(row);
  }
  if(row[13]==2)
  {
    shtB.appendRow(row);
  }
}

我确实认为如果onFormSubmits加速,这种方法可能无法跟上并可能得到错误的数据。我会考虑添加另一个列调用它复制并运行所有行并复制那些在复制列中没有TimeStamp的行。循环版本是下面的代码。

function copyToSpreadsheetWithLoop()
{
  var CopiedColumn=20;//Don't really know
  var ss0=SpreadsheetApp.openById('1q7HyUL3bhd_gLEAfVJrH4strnkF9_UP6uhY4Qy5A6Vc');
  var sht0=ss0.getSheetByName('Sheet1');
  var rng0=sht0.getDataRange();
  var rng0A=rng0.getValues();
  var ssA=SpreadsheetApp.openById('1sWSSHOgdum0LtCggtM6Gz-ytmFJJjMXH9pZ0NYGMV7g');
  var shtA=ssA.getSheetByName('Sheet1');
  var ssB=SpreadsheetApp.openById('1sWSSHOgdum0LtCggtM6Gz-ytmFJJjMXH9pZ0NYGMV7g');
  var shtB=ssB.getSheetByName('Sheet1');
  for(var i=1;i<rng0A.length;i++)
  {
    var row= rng0A[i];
    if(row[CopiedColumn-1])
    {
      if(row[13]==1)
      {
        shtA.appendRow(row);
        sht0.getRange(i+1,CopiedColumn).setValue(Utilities.formatDate(new Date(), "GMT-6", "dd/MM/yyyy HH:mm:ss"));
      }
      if(row[13]==2)
      {
        shtB.appendRow(row);
        sht0.getRange(i+1,CopiedColumn).setValue(Utilities.formatDate(new Date(), "GMT-6", "dd/MM/yyyy HH:mm:ss"));
      }
    }
  }
}