简体   繁体   English

使用第三张工作表中的单元格值将Google工作表复制到新工作表中

[英]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.

相关问题 使用Google Apps脚本将单元格的值从工作表2自动复制到工作表1 - auto copy value of cell from sheet 2 to sheet 1 using google apps script 将基于单元格值的行数据复制到 Google 表格中的新工作表 - Copy row data based on cell value to new sheet in Google Sheets 根据单元格中的值将行复制到新工作表 - Copy a row to new sheet based on value in a cell Google文件-指令码,将储存格从一张工作表复制到另一张工作表 - Google Docs - Scripts, copy cell from one sheet to another sheet 使用 Google 脚本从 Google 表格上的特定单元格命名新表格 - Naming a New Sheet with Google Scripts from a specific cell on a Google Sheet 谷歌工作表脚本 - 复制并放置一个单元格值 - Google sheet script - copy & plase a cell value 谷歌工作表脚本 - 在将工作表复制到新文件后,将新工作表 ID 写回单元格 - google sheet script - writing back new sheet ID to cell, after copy sheet to new file 用于从源单元格中独立复制值的 Google 工作表脚本 - Google sheet script to copy a value indipendently from the source cell 将工作表中的值和格式复制到新的Google电子表格文档中? - Copy value and format from a sheet to a new google Spreadsheet document? 使用脚本将值和格式复制到Google工作表中的新工作表 - Copy Value and Format to new sheet in Google sheets using a script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM