[英]Copy Google sheet to a new sheet using cell value from 3rd sheet
I've been looking through the forums trying to search for the right way to do this but I can't seem to get it right. 我一直在浏览论坛,试图寻找正确的方法来实现此目的,但我似乎无法正确地做到这一点。 This is my first time using Google Apps Script so I'm learning as I'm going. 这是我第一次使用Google Apps脚本,因此我正在学习。
This is what I'm trying to do. 这就是我想要做的。
1) I want to duplicate all the contents of an old sheet into a new sheet. 1)我想将旧工作表的所有内容复制到新工作表中。 The script will determine the existing sheet name by referring to the cell b17 from 'Statistics' sheet, and will duplicate the old sheet, including values formats, references and formulas. 该脚本将通过引用“统计”工作表中的单元格b17来确定现有工作表名称,并将复制旧工作表,包括值格式,引用和公式。 The duplicated sheet will be named from cell c17 of the 'Statistics' sheet. 重复的工作表将从“统计”工作表的单元格c17中命名。
2) If possible I would also like to make the old sheet remove all the references and just keep the values and formats after the new sheet has been created. 2)如果可能的话,我还要使旧表删除所有引用,并在创建新表后保留值和格式。 Similar to the copy all and ctrl-shift-v, except automating it in one fell swoop. 与全部复制和ctrl-shift-v相似,不同之处在于自动完成一次复制。
This is the code i currently have, not sure if it is correct 这是我目前拥有的代码,不确定是否正确
function newSheetLast() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Statistics").activate();
var oldSheetName = ss.getRange('b17').getDisplayValue();
var newSheetName = ss.getRange('c17').getDisplayValue();
var sheet = ss.getSheetByName(oldSheetName).activate();
var source = SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet(newSheetName);
};
I am getting stuck when trying to do 1. I tried the copyTo and can get the sheet to copy, but loose all the formatting and formulas when i copy over to the new sheet. 尝试执行1时,我陷入了困境。尝试了copyTo并可以复制工作表,但是当我复制到新工作表时,将丢失所有格式和公式。
I am not sure how to even attempt 2 我不确定如何尝试2
You variable naming and the way duplicating sheets works is a bit confusing. 您的变量命名和重复工作表的工作方式有些混乱。 You should really be able to duplicate a Sheet Object directly or at least be able to try and supply the name of the sheet you are trying to duplicate but no, it is on a Spreadsheet level and only works on the active sheet... 您确实应该能够直接复制工作表对象,或者至少能够尝试提供您要复制的工作表的名称,但是不能,它在电子表格级别并且仅在活动工作表上起作用。
Duplicating a sheet carries formulae and formatting. 复制工作表包含公式和格式。 Overwriting with static values can be done with setValues(getValues)
. 可以使用setValues(getValues)
完成静态值的覆盖。
function newSheetLast() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var settings = ss.getSheetByName("Sheet1");
var oldSheetName = settings.getRange('B17').getDisplayValue();
var oldSheet = ss.getSheetByName(oldSheetName);
var newSheetName = settings.getRange('C17').getDisplayValue();
ss
.getSheetByName(oldSheetName)
.activate()
.getParent()
.duplicateActiveSheet()
.setName(newSheetName);
oldSheet.getDataRange().setValues(oldSheet.getDataRange().getValues());
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.