Google apps script 从电子表格复制到另一个具有动态标题的电子表格
我目前正在从事一个项目,我需要将数据从电子表格中的一个特定工作表复制到另一个具有动态标题(带日期的文本)的电子表格。 使用ID或名称移动数据是可以的,但使用新创建的电子表格似乎有些迟钝。 所有功能都将在同一个应用程序脚本中: 创造 抄袭 说明:Google apps script 从电子表格复制到另一个具有动态标题的电子表格,google-apps-script,google-sheets,google-sheets-api,sourceforge-appscript,Google Apps Script,Google Sheets,Google Sheets Api,Sourceforge Appscript,我目前正在从事一个项目,我需要将数据从电子表格中的一个特定工作表复制到另一个具有动态标题(带日期的文本)的电子表格。 使用ID或名称移动数据是可以的,但使用新创建的电子表格似乎有些迟钝。 所有功能都将在同一个应用程序脚本中: 创造 抄袭 说明: 返回一个对象,这样您就可以直接使用此函数的输出,而不需要额外的代码 新生成的电子表格将有一张名为Sheet1的表格,与手动创建新电子表格文件时相同。因此,您可以使用该功能将工作表的名称更改为Target。此外,此函数还返回一个对象(targetShe
- 返回一个对象,这样您就可以直接使用此函数的输出,而不需要额外的代码
- 新生成的电子表格将有一张名为
的表格,与手动创建新电子表格文件时相同。因此,您可以使用该功能将工作表的名称更改为Sheet1
。此外,此函数还返回一个对象(Target
),然后可用于设置值targetSheet
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);
}
如果要将titlesDate
用作将由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);
}
// 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);
}