[英]Is there a way to make many copies of a spreadsheet file and choose the name for each file in Google Apps Script
So, as of now, I have the below function that definitely works to make as many copies of a Google Sheets as possible.所以,截至目前,我有以下功能绝对可以制作尽可能多的 Google 表格副本。 For ex, below will make 3 copies of a Google Sheets (in reality, I'll want to make 40+ copies).
例如,下面将制作 3 个 Google 表格的副本(实际上,我想要制作 40 多个副本)。
What I'm having trouble with is finding a way to automatically change their file names, so that it's each person's name and then " - System" for each copy.我遇到的问题是找到一种方法来自动更改他们的文件名,以便它是每个人的名字,然后是每个副本的“-系统”。
This is what I currently have:这是我目前拥有的:
function copyDocs() {
for(i=0; i<3; i++){
var drive=DriveApp.getFileById('SheetsID');
drive.makeCopy();
}
}
I'd want to do something like this:我想做这样的事情:
var people = Array("Johnson Henry", "Person Last", "Tierra Smith")
function copyDocs() {
for(i=0; i<3; i++){
var drive=DriveApp.getFileById('SheetsID');
drive.makeCopy(people[i] & " - System");
}
}
Do any of you know of a way to modify my code to do this?你们中有人知道修改我的代码以执行此操作的方法吗? Note that I'm a beginner at Apps Script, so please forgive my lack of knowledge here.
请注意,我是 Apps Script 的初学者,所以请原谅我在这里缺乏知识。
You can do that in a more efficient way.你可以以更有效的方式做到这一点。
You can use forEach() to iterate through the elements of the people
array and then use template literals to name the files accordingly:您可以使用forEach()遍历
people
数组的元素,然后使用模板文字相应地命名文件:
function copyDocs() {
const drive=DriveApp.getFileById('SpreadsheetID');
const people = ["Johnson Henry", "Person Last", "Tierra Smith"];
people.forEach(person=>{
var copy = drive.makeCopy( `${person} - System`)
var copySpreadsheet = SpreadsheetApp.openById(copy.getId());
var copySheet = copySpreadsheet.getSheets()[0];
copySheet.getRange('B2').setValue(person);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.