【问题标题】:Google App Scripts - Copy file into folder with names from rangesGoogle App Scripts - 将文件复制到具有范围名称的文件夹中
【发布时间】:2017-03-01 00:03:56
【问题描述】:

我正在使用 Google 表格,我正在尝试创建一个脚本,该脚本将对当前文件制作一定数量的副本,为每个副本提供一个范围内名称列表中的下一个名称,并将这些由先前脚本创建的文件夹中的文件。我能够让这一切正常工作,但仅限于第一个文件(6 个,可能更多)并且无法弄清楚我做错了什么。 Here's a copy of the sheet。我也有另一个版本,它仅用于创建文档的副本,但我正在尝试为我的最终用户简化流程,他们可能正在创建数十个副本,并希望为他们进行组织会有所帮助。

感谢您的帮助!

这是脚本:

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;
}
}

【问题讨论】:

    标签: google-apps-script google-sheets google-apps


    【解决方案1】:

    你在正确的轨道上。 我在下面修改了你,并附有解释:

    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;
    }
    }
    

    我修改了代码,因为你从 getValues 获得了一个二维数组

    //The below statements a 2D dimensional array.
    
    var CoachObjects = CoachNames.getValues();
    

    要访问单个值,您将使用这样的语句

    `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();
    

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多