如何从一个 Google Sheet 选项卡 copy/paste 到另一个 google sheet 并在目标 google [=15= 中指定选项卡]?
How do you copy/paste from one Google Sheet Tab, to another google sheet and specify the tab in the target google sheet?
我正在为 Google Sheet 文档创建一个 Google 应用程序脚本。我需要允许用户 select 源 Google Sheet 中的一系列单元格,然后能够在目标 google sheet 中绘制特定选项卡],将该数据粘贴到。
我已经能够硬编码 TARGET 选项卡,它在另一个 google sheet 中,但我无法弄清楚如何制作它以便用户可以选择以这种方式将数据复制到的特定选项卡。
这是我第一次尝试编码。我是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);
}
下面的代码将自定义菜单添加到 sheet,其中一个项目在单击时会显示获取目标 sheet 名称的提示。输入它,它将当前 sheet 活动范围复制到目标 sheet 最后一行。保存并重新加载 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);
}
}
我正在为 Google Sheet 文档创建一个 Google 应用程序脚本。我需要允许用户 select 源 Google Sheet 中的一系列单元格,然后能够在目标 google sheet 中绘制特定选项卡],将该数据粘贴到。
我已经能够硬编码 TARGET 选项卡,它在另一个 google sheet 中,但我无法弄清楚如何制作它以便用户可以选择以这种方式将数据复制到的特定选项卡。
这是我第一次尝试编码。我是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);
}
下面的代码将自定义菜单添加到 sheet,其中一个项目在单击时会显示获取目标 sheet 名称的提示。输入它,它将当前 sheet 活动范围复制到目标 sheet 最后一行。保存并重新加载 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);
}
}