简体   繁体   English

如何从一个Google工作表标签复制/粘贴到另一个Google工作表并指定目标Google工作表中的标签?

[英]How do you copy/paste from one Google Sheet Tab, to another google sheet and specify the tab in the target google sheet?

I'm creating a Google App Script, for a Google Sheet Document. 我正在为Google Sheet Document创建Google App脚本。 I need to allow a user to select a range of cells in the Source Google Sheet and then be able to pic the specific tab in the target google sheet, to paste that data to. 我需要允许用户在源Google表格中选择一系列单元格,然后才能拍摄目标Google工作表中的特定标签,以便将该数据粘贴到。

I've been able to hardcode a TARGET tab, which in another google sheet, but I have not been able to figure out how to make it so that the user can pick the specific tab that way to copy the data to. 我已经能够硬编码TARGET选项卡,这在另一个google工作表中,但我无法弄清楚如何制作它,以便用户可以选择特定的选项卡以复制数据。

This is my first endeavor into coding. 这是我第一次尝试编码。 I am a 100% novice. 我是100%的新手。

function GeneralToTracking() {

  /*
  This code defines the Source Google Sheet Doc and the Target Google Sheet Doc. These are two
  different google sheet docs. They are NOT 2 sheets in the same google sheet doc.
  */

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("1yxfpC7Yag9GAkoe5BUjjg12cUhGmGr5ryeGl87JmZqU");

  /*
  This code is to pick specific sheets within the Source & Target Sheet.
     Source Google Sheet = "New Stuff"
     Target Google Sheet = "Archive"
  */

  var source_sheet = ss.getActiveSheet();
  var target_sheet = target.getSheetByName("Archive"); // ++++ TO DO: Need to present the user with a list of tabs in the Target doc. Prompt w/ Radio Buttons. ++++

  /* 
  This code determines the from-range and the to-range to copy & says where to put it in the Target.
  */

  var source_range = source_sheet.getActiveRange();
  var last_row = target_sheet.getLastRow();

//  source_range.copyTo(target_range);

  if (last_row > 0) target_sheet.insertRowAfter(last_row);
  var target_range = target_sheet.getRange(last_row + 1, 1);
  var copiedsheet = source_sheet.copyTo(target);
  copiedsheet.getRange(source_range.getA1Notation()).copyTo(target_range);
  target.deleteSheet(copiedsheet);

}

Below code adds a custom menu to sheet, with an item which upon click shows a prompt to get the target sheet name. 下面的代码将一个自定义菜单添加到工作表,其中一个项目在单击时显示提示以获取目标工作表名称。 Enter that and it copies current sheet active range to target sheet last row. 输入它并将当前工作表活动范围复制到目标工作表的最后一行。 Save and reload sheet. 保存并重新加载表格。

// ------------ GENERAL GOOGLE SHEET DOC TO TRACKING GOOGLE SHEET DOC ------------

function GeneralToTracking(tName) {
  /*
  This code defines the Source Google Sheet Doc and the Target Google Sheet Doc. These are two
  different google sheet docs. They are NOT 2 sheets in the same google sheet doc.
  */

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById('1yxfpC7Yag9GAkoe5BUjjg12cUhGmGr5ryeGl87JmZqU');

  /*
  This code is to pick specific sheets within the Source & Target Sheet.
     Source Google Sheet = "New Stuff"
     Target Google Sheet = "Archive"
  */

  var source_sheet = ss.getActiveSheet(); // ++++ TO DO: Need to make this work on whatever sheet the user is on currently, instead of being hardcoded. ++++
  var target_sheet = target.getSheetByName(tName); // ++++ TO DO: Need to present the user with a list of tabs in the Target doc. Prompt w/ Radio Buttons. ++++

  /* 
  This code determines the from-range and the to-range to copy & says where to put it in the Target.
  */

  var source_range = source_sheet.getActiveRange();
  var sValues = source_range.getValues();
  var last_row = target_sheet.getLastRow();

  //  source_range.copyTo(target_range);

  if (last_row > 0) target_sheet.insertRowAfter(last_row);
  var target_range = target_sheet.getRange(last_row + 1, 1, sValues.length, sValues[0].length);
  target_range.setValues(sValues);

  // double check and enable these when above test is pass
  // var copiedsheet = source_sheet.copyTo(target);
  // copiedsheet.getRange(source_range.getA1Notation()).copyTo(target_range);
  // target.deleteSheet(copiedsheet);
}

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
    .createMenu('Custom Menu')
    .addItem('Enter Target Name', 'enterTargetName')
    .addToUi();
}

function enterTargetName() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.prompt(
    'Enter Target Sheet Name',
    'Please enter target sheet name:',
    ui.ButtonSet.OK_CANCEL
  );

  // Process the user's response.
  var button = result.getSelectedButton();
  var text = result.getResponseText();
  if (button == ui.Button.OK) {
    // User clicked "OK".
    GeneralToTracking(text);
  }
}

暂无
暂无

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

相关问题 将一个标签上的谷歌工作表中的值复制到另一个标签 - Copy values from google sheet on one tab to another tab 如何将一行从谷歌工作表中的一个选项卡移动到同一个工作表中的另一个选项卡 - how do I move a row from one tab in a google sheet to another in the same sheet 自动将值从一个谷歌工作表复制并粘贴到另一个 - Automatically copy and paste values from one google sheet to another 如何使用 Google App Script 将一个 Google Sheet 中的所有数据提取到另一个 Google Sheet Tab? - How do I pull All data in one Google Sheet to another Google Sheet Tab using Google App Script? 如何复制 google 工作表标签并将其重命名 - How do I make a copy of a google sheet tab and rename it 如何将一个Google工作表复制到另一个Google工作表 - How to copy one google sheet to different another google sheet 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? Google文件-指令码,将储存格从一张工作表复制到另一张工作表 - Google Docs - Scripts, copy cell from one sheet to another sheet 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet 如何将一张工作表的值复制到另一张工作表Google工作表脚本 - How to copy the value of one sheet to another sheet Google sheets Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM