[英]copy from spreadsheet to another spreadsheet with dynamic title
I'm currently working on a project where to I need to copy data from one specific sheet in a spreadsheet to another spreadsheet with a dynamic title ( text with date).我目前正在做一个项目,我需要将数据从电子表格中的一个特定工作表复制到另一个具有动态标题(带日期的文本)的电子表格。 moving the data with ID or by name is ok but with a freshly created spreadsheet seems tuff.
使用 ID 或名称移动数据是可以的,但使用新创建的电子表格似乎很困难。 all the function will be in the same app script: creation copy
所有 function 都将在同一个应用程序脚本中:创建副本
function titleAsDate() {
var currentDate = Utilities.formatDate(new Date(), "GMT+8", "dd-MM-yyyy HH:mm:ss")
SpreadsheetApp.create("Report of the " + currentDate)
}
function copyWithValues() {
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
let sourceSheet = spreadSheet.getSheetByName('Sources');
let sourceRange = sourceSheet.getDataRange();
let sourceValues = sourceRange.getValues();
let rowCount = sourceValues.length;
let columnCount = sourceValues[0].length;
let targetSheet = spreadSheet.getSheetById('Target');
let targetRange = targetSheet.getRange(1, 1, rowCount, columnCount);
targetRange.setValues(sourceValues);
}
SpreadsheetApp.create(name) returns a spreadsheet object so you can directly use the output of this function directly without the need of extra code. SpreadsheetApp.create(name)返回一个电子表格object,因此您可以直接使用此 function 的 output 而无需额外的代码。
The newly generated spreadsheet will have one sheet with the name Sheet1
as when you manually create a new spreadsheet file.新生成的电子表格将有一个名为
Sheet1
的工作表,就像您手动创建新的电子表格文件一样。 Therefore, you can use the sheet.setName(name) function to change the name of the sheet to Target
.因此,您可以使用sheet.setName(name) function 将工作表的名称更改为
Target
。 Also this function returns a sheet object ( targetSheet
) which can then be used to set the values.此 function 还返回一个工作表object (
targetSheet
),然后可以使用它来设置值。
This is all the code in one function:这是一个 function 中的所有代码:
function copyWithValues() {
const spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
const sourceSheet = spreadSheet.getSheetByName('Sources');
const sourceRange = sourceSheet.getDataRange();
const sourceValues = sourceRange.getValues();
const currentDate = Utilities.formatDate(new Date(), "GMT+8", "dd-MM-yyyy HH:mm:ss"); // new code
const targetSpreadsheet = SpreadsheetApp.create("Report of the " + currentDate); // new code
let rowCount = sourceValues.length;
let columnCount = sourceValues[0].length;
let targetSheet = targetSpreadsheet.getSheetByName('Sheet1').setName("Target"); // new code
let targetRange = targetSheet.getRange(1, 1, rowCount, columnCount);
targetRange.setValues(sourceValues);
}
If you want to use titleAsDate
as a helper function which will be called by copyWithValues
, then you can use this code and execute only copyWithValues
:如果您想使用
titleAsDate
作为助手 function 将由copyWithValues
调用,那么您可以使用此代码并仅执行copyWithValues
:
// helper function, used by copyWithValues
function titleAsDate() {
const currentDate = Utilities.formatDate(new Date(), "GMT+8", "dd-MM-yyyy HH:mm:ss");
return SpreadsheetApp.create("Report of the " + currentDate); // new code
}
// main function, you should execute this function
function copyWithValues() {
const spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
const sourceSheet = spreadSheet.getSheetByName('Sources');
const sourceRange = sourceSheet.getDataRange();
const sourceValues = sourceRange.getValues();
const targetSpreadsheet = titleAsDate(); // new code
let rowCount = sourceValues.length;
let columnCount = sourceValues[0].length;
let targetSheet = targetSpreadsheet.getSheetByName('Sheet1').setName("Target"); // new code
let targetRange = targetSheet.getRange(1, 1, rowCount, columnCount);
targetRange.setValues(sourceValues);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.