Google apps script 谷歌应用程序脚本-将文件复制到文件夹中,文件名范围为

Google apps script 谷歌应用程序脚本-将文件复制到文件夹中,文件名范围为,google-apps-script,google-sheets,google-apps,Google Apps Script,Google Sheets,Google Apps,我在Google Sheets中工作,我正在尝试创建一个脚本,该脚本将生成当前文件的一定数量的副本,从范围内的名称列表中为每个副本指定下一个名称,并将这些文件放在由以前的脚本创建的文件夹中。我能够让它全部正常工作,但只针对第一个文件(6个文件中的一个,可能更多),并且无法找出我做错了什么。 . 我还有另一个版本,它只用于创建文档的副本,但我正在尝试为我的最终用户简化流程,他们可能正在创建几十个副本,并希望为他们进行组织将有所帮助 谢谢你的帮助 以下是脚本: function createcopi

我在Google Sheets中工作,我正在尝试创建一个脚本,该脚本将生成当前文件的一定数量的副本,从范围内的名称列表中为每个副本指定下一个名称,并将这些文件放在由以前的脚本创建的文件夹中。我能够让它全部正常工作,但只针对第一个文件(6个文件中的一个,可能更多),并且无法找出我做错了什么。 . 我还有另一个版本,它只用于创建文档的副本,但我正在尝试为我的最终用户简化流程,他们可能正在创建几十个副本,并希望为他们进行组织将有所帮助

谢谢你的帮助

以下是脚本:

function createcopies2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();

// Get the range of cells that store necessary data.
var CoachNames = ss.getRangeByName("CoachNames");

var CoachObjects = CoachNames.getValues();

var schoolNames = ss.getRangeByName("SchoolNames");

var SchoolObjects = schoolNames.getValues();

var id = ss.getId();  
// The next variable gets a value that counts how many CoachNames there are in the spreadsheet
var coaches = ss.getRangeByName("Coaches");

var numcoaches = coaches.getValue();

//here's the function
for(i=0; i<numcoaches; i++){ 

var drive=DriveApp.getFileById(id);

var name=CoachObjects[i].toString();

var folder=DriveApp.getFoldersByName(SchoolObjects[i]).next();

var folderid=folder.getId();

var folder2=DriveApp.getFolderById(folderid)
if(folder) {
drive.makeCopy(name, folder2)}
else{
drive.makeCopy(name);
}

return;
}
}
函数createcopies2(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
//获取存储必要数据的单元格范围。
var CoachNames=ss.getRangeByName(“CoachNames”);
var CoachObjects=CoachNames.getValues();
var schoolNames=ss.getRangeByName(“schoolNames”);
var SchoolObjects=schoolNames.getValues();
var id=ss.getId();
//下一个变量获取一个值,该值统计电子表格中有多少个辅助参数
var coach=ss.getRangeByName(“coach”);
var numcoaches=coach.getValue();
//下面是函数

对于(i=0;i而言,您走在正确的轨道上。 我已在下面对您进行了修改,并进行了解释:

function createcopies2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();

// Get the range of cells that store necessary data.
var CoachNames = ss.getRangeByName("CoachNames");
//The below statements a 2D dimensional array.
//To access the individual value you will have a statement like this
//CoachObjects[0][0],CoachObject[1][0],[2][0] ..., down the row
var CoachObjects = CoachNames.getValues();

var schoolNames = ss.getRangeByName("SchoolNames");
//The below statements a 2D dimensional array.
var SchoolObjects = schoolNames.getValues();

var id = ss.getId();  
// The next variable gets a value that counts how many CoachNames there are in the spreadsheet
var coaches = ss.getRangeByName("Coaches");

var numcoaches = coaches.getValue();
//Moved the below statement out of the loop
// Baceause you are using the same file
var drive=DriveApp.getFileById(id);
//here's the function
for(i=0; i<numcoaches; i++){ 
var name=CoachObjects[i][0].toString();

var folder=DriveApp.getFoldersByName(SchoolObjects[i][0]).next();

var folderid=folder.getId();

var folder2=DriveApp.getFolderById(folderid)
if(folder) {
drive.makeCopy(name, folder2)}
else{
drive.makeCopy(name);
}

return;
}
}
要访问单个值,您将使用如下语句

`CoachObjects[0][0]`
 CoachObjects[1][0]
 .......     [2][0] ...
 down the row
此外,这些是冗余的代码行:

var folder=DriveApp.getFoldersByName(SchoolObjects[i][0]).next();
var folderid=folder.getId();
var folder2=DriveApp.getFolderById(folderid)
你可以把它换成

var folder2=DriveApp.getFoldersByName(SchoolObjects[i][0]).next();
var folder2=DriveApp.getFoldersByName(SchoolObjects[i][0]).next();