简体   繁体   English

将行从一张工作表复制到另一张在该行中链接的工作表

[英]Copy Row from one Sheet to another sheet which is linked in the row

I have previously been able to copy rows from one Spreadsheet to another, however what I am trying to do now is copy a row to a specific spreadsheet that is linked on the same row and I can't seem to work it out.我以前能够将行从一个电子表格复制到另一个,但是我现在要做的是将一行复制到链接在同一行上的特定电子表格中,但我似乎无法解决。

I have around 500 rows of names with data (I cannot share a sheet as my firms Google does not allow for sharing externally but I have a screenshot of dummy data below).我有大约 500 行包含数据的名称(我无法共享表格,因为我的公司 Google 不允许与外部共享,但我在下面有一个虚拟数据的屏幕截图)。 I used a formula to find the Unique names and then created a Google Sheet for each person and linked the Sheet back to the master data.我使用公式查找唯一名称,然后为每个人创建了一个 Google 表格,并将表格链接回主数据。

Master Data Sheet (tab is called Sheet1)主数据表(选项卡称为 Sheet1)

I am looking for a script that will work through the sheet, copying the rows to the link that is on the same row but I just can't figure it out - the following script does not work but I am at a loss so any help would be appreciated.我正在寻找一个可以通过工作表工作的脚本,将行复制到同一行上的链接,但我无法弄清楚 - 以下脚本不起作用但我不知所措,所以任何帮助将不胜感激。 Apologies if I haven't explained myself very well!!抱歉,如果我没有很好地解释自己!

function copyTo(){

var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("Sheet1");

var data = srcSheet.getDataRange().getValues();
for(var i = 1; i < data.length; i++) {
  var row = data[i];
  var link = row[3];
  var id = row[4];
  var complete = row[5];

  var COMPLETE = "Complete"

  if(link !== "" && complete !== "Complete"){

    var srcRange = srcSheet.getRange("A" + i + ":C" + i);

  var linkID = id.toString()

  var tarSS = DriveApp.getFileById(linkID);
  var tarSheet = tarSS.getSheetbyName("Sheet1");

   var tarRow = tarSheet.getLastRow();
  //tarSheet.insertRowAfter(tarRow);
  var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":C" + (tarRow+1));
  
  srcRange.copyTo(tarRange);
  srcSheet.getRange(i+1,6).setValue(COMPLETE)

 }
 }};

Try this:尝试这个:

function copyTo() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName("Sheet1");
  var vs = sh.getDataRange().getValues();
  for (var i = 1; i < vs.length; i++) {
    var row = vs[i];
    var link = row[3];
    var id = row[4];
    var complete = row[5];
    var COMPLETE = "Complete"
    if (link !== "" && complete !== "Complete") {
      var rg = sh.getRange("A" + i + ":C" + i);
      var linkID = id.toString()
      var tarSS = SpreadsheetApp.openById(linkID);
      var tarSheet = tarSS.getSheetbyName("Sheet1");
      var tarRow = tarSheet.getLastRow();
      var tarRange = tarSheet.getRange("A" + (tarRow + 1) + ":C" + (tarRow + 1));
      rg.copyTo(tarRange);
      sh.getRange(i + 1, 6).setValue(COMPLETE)
    }
  }
};

As much as I understand you question here is the script which will work for you exactly you want.据我了解,您的问题是完全适合您的脚本。 This will copy data from sheet you want and then create a new spreadsheet and paste into that sheet then link will show of this in same sheet from data copied这将从您想要的工作表中复制数据,然后创建一个新的电子表格并粘贴到该工作表中,然后链接将从复制的数据显示在同一张工作表中

Let me know how this worked.让我知道这是如何工作的。

Code.gs代码.gs

const sendDataToSheet = () => {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const ws = ss.getSheetByName(''paste here your sheet name'') 
  const newSheet = SpreadsheetApp.create("give new sheet name here")   
  ws.copyTo(newSheet).setName("Copied Data")   
  const sheetURL = newSheet.getUrl()
  ws.insertRowBefore(1) 
  ws.getRange(1,1).setValue(sheetURL)

}

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

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