简体   繁体   English

有没有办法制作电子表格文件的多个副本并在 Google Apps Script 中为每个文件选择名称

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

相关问题 按名称将文件移动到文件夹-Google Apps脚本 - Move file to folder by name - Google apps script 是否可以使用 Google Apps 脚本向主 Google 电子表格的副本添加不同的编辑器? - Is it possible to use Google Apps Script to add different editors to copies of a master Google Spreadsheet? Google Spreadsheet:如何使用Google Apps脚本命名范围 - Google Spreadsheet: How to name a range using Google Apps Script 如何在谷歌应用脚本中部分更改文件名? - How to partially change a file name in google apps script? 将 json/csv 文件(从 API 获取)保存到电子表格文档而不使用循环 - Google Apps 脚本 - Save a json/csv file (fetched from an API) to a spreadsheet document without using a loop - Google Apps Script 无法将我的 output 文件保存为基于谷歌应用脚本中电子表格单元格的名称 - Having trouble save my output file as name based on cell from spreadsheet in google apps scripts 如何在电子表格文件中的所有工作表上运行 Google Apps 脚本? - How do I run a Google Apps script on all sheets in my spreadsheet file? 谷歌应用程序脚本 html 到电子表格 - Google apps script html to spreadsheet 使用来自谷歌电子表格的脚本创建一个 xml 文件 - Creating an xml file with a script from google spreadsheet 有没有办法将谷歌电子表格保存为数据库中的blob然后,在应用程序脚本中,打开该blob作为电子表格 - Is there a way to save a google spreadsheet as a blob in a database and then, during apps script, open that blob as a spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM