2016-03-10 78 views
0

我剛碰到一些麻煩。預先感謝您的任何幫助。我只是用一個if語句更新了一個腳本,我需要添加一個新的表單來響應。我的目標是根據表單響應中的值複製文件夾以及兩個不同「源」文件夾的子文件夾和文件。例如,如果響應是「Production」,那麼如果響應是「Creative」,則複製「TestTempFolder」,然後將「TestTempFolder2」複製到已創建的新文件夾中,並移動到存儲所有項目的「Projects」文件夾。我不確定我做錯了什麼。這裏是我更新的腳本:Google App腳本根據表單的響應值複製文件夾和文件

function start() { 

var ss = SpreadsheetApp.getActiveSpreadsheet();//Returns the currently active spreadsheet, or null if there is none. 
var sheet = ss.getActiveSheet();//Gets the active sheet in a spreadsheet. 
var lastRow = sheet.getLastRow();//Returns the position of the last row that has content. 
var projectTypeRange = sheet.getRange(lastRow,2); 
var projectType = projectTypeRange.getValues(); 

if (projectType == 'Production'){ 
productionCopy(); 
} 
else if (projectType == 'Creative'){ 
creativeCopy(); 
} 


function productionCopy(){ 

var ss = SpreadsheetApp.getActiveSpreadsheet();//Returns the currently active spreadsheet, or null if there is none. 
var sheet = ss.getActiveSheet();//Gets the active sheet in a spreadsheet. 
var lastRow = sheet.getLastRow();//Returns the position of the last row that has content. 
var projectNameRange = sheet.getRange(lastRow, 3);//Returns the range with the top left cell at the given coordinates. 
var projectName = projectNameRange.getValues();//Returns the value of the top-left cell in the range. 
var sh = SpreadsheetApp.getActiveSheet() 
var startcell = sh.getRange('E2').getValue(); 
var colValues = sh.getRange('E2:E').getValues();// get all the values in column A in an array 
var max=0;// define the max variable to a minimal value 
for(var r in colValues){ // iterate the array 
    var vv=colValues[r][0].toString().replace(/[^0-9]/g,'');// remove the letters from the string to convert to number 
if(Number(vv)>max){max=vv};// get the highest numeric value in th column, no matter what happens in the column... this runs at array level so it is very fast 
} 
max++ ; // increment to be 1 above max value 
sh.getRange(sh.getLastRow(), 5).setValue(Utilities.formatString('CG%06d',max));// and write it back to sheet's last row. 
var projectIdRange = sheet.getRange(lastRow, 5); 
var projectId = projectIdRange.getValues();//Returns the value of the top-left cell in the range. 

var clientNameRange = sheet.getRange(lastRow,3); 
var clientName = clientNameRange.getValues(); 
var curDate = Utilities.formatDate(new Date(), "GMT+1", "yyyy") 
var productionSourceFolder = "TestTempFolder"; 
var targetFolder = "target"; 
var source = DriveApp.getFoldersByName(productionSourceFolder); 
var target = DriveApp.createFolder(projectId + "_" + projectName + "_" + clientName + "_" + curDate); 

if (source.hasNext()) { 
copyFolder(source.next(), target); 
} 

DriveApp.getFolderById('0BwrizIzPM38bUGFPV2E1Q0JSMms').addFolder(target); 
DriveApp.removeFolder(target); 

function copyFolder(source, target) { 
var folders = source.getFolders(); 
var files = source.getFiles(); 

while(files.hasNext()) { 
var file = files.next(); 
file.makeCopy(file.getName(), target); 
} 

while(folders.hasNext()) { 
var subFolder = folders.next(); 
var folderName = subFolder.getName(); 
var targetFolder = target.createFolder(folderName); 
copyFolder(subFolder, targetFolder); 
} 

} 

function creativeCopy(){ 

var ss = SpreadsheetApp.getActiveSpreadsheet();//Returns the currently active spreadsheet, or null if there is none. 
var sheet = ss.getActiveSheet();//Gets the active sheet in a spreadsheet. 
var lastRow = sheet.getLastRow();//Returns the position of the last row that has content. 
var projectNameRange = sheet.getRange(lastRow, 3);//Returns the range with the top left cell at the given coordinates. 
var projectName = projectNameRange.getValues();//Returns the value of the top-left cell in the range. 
var sh = SpreadsheetApp.getActiveSheet() 
var startcell = sh.getRange('E2').getValue(); 
var colValues = sh.getRange('E2:E').getValues();// get all the values in column A in an array 
var max=0;// define the max variable to a minimal value 
for(var r in colValues){ // iterate the array 
    var vv=colValues[r][0].toString().replace(/[^0-9]/g,'');// remove the letters from the string to convert to number 
if(Number(vv)>max){max=vv};// get the highest numeric value in th column, no matter what happens in the column... this runs at array level so it is very fast 
} 
max++ ; // increment to be 1 above max value 
sh.getRange(sh.getLastRow(), 5).setValue(Utilities.formatString('CG%06d',max));// and write it back to sheet's last row. 
var projectIdRange = sheet.getRange(lastRow, 5); 
var projectId = projectIdRange.getValues();//Returns the value of the top-left cell in the range. 
var clientNameRange = clientNameRange.getRange(lastRow,3); 
var clientName = clientNameRange.getValues(); 
var curDate = Utilities.formatDate(new Date(), "GMT+1", "yyyy") 
var creativeSourceFolder = "TestTempFolder2"; 
var targetFolder = "target"; 
var source = DriveApp.getFoldersByName(creativeSourceFolder); 
var target = DriveApp.createFolder(projectId + "_" + projectName + "_" + clientName + "_" + curDate); 

if (source.hasNext()) { 
copyFolder(source.next(), target); 
} 

DriveApp.getFolderById('0BwrizIzPM38bUGFPV2E1Q0JSMms').addFolder(target); 
DriveApp.removeFolder(target); 

var folders = source.getFolders(); 
var files = source.getFiles(); 

while(files.hasNext()) { 
var file = files.next(); 
file.makeCopy(file.getName(), target); 
} 

while(folders.hasNext()) { 
var subFolder = folders.next(); 
var folderName = subFolder.getName(); 
var targetFolder = target.createFolder(folderName); 
copyFolder(subFolder, targetFolder); 
} 
} 
} 
} 

我現在得到這個錯誤:ReferenceError:「creativeCopy」未定義。 (第13行,文件「代碼」)。我不知道我在做什麼錯誤的任何幫助將不勝感激。

回答

0

你有一些括號不匹配。

試試這個。

function start() { 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); //Returns the currently active spreadsheet, or null if there is none. 
    var sheet = ss.getActiveSheet(); //Gets the active sheet in a spreadsheet. 
    var lastRow = sheet.getLastRow(); //Returns the position of the last row that has content. 
    var projectTypeRange = sheet.getRange(lastRow, 2); 
    var projectType = projectTypeRange.getValues(); 

    if (projectType == 'Production') { 
     productionCopy(); 
    } else if (projectType == 'Creative') { 
     creativeCopy(); 
    } 
} 

function productionCopy() { 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); //Returns the currently active spreadsheet, or null if there is none. 
    var sheet = ss.getActiveSheet(); //Gets the active sheet in a spreadsheet. 
    var lastRow = sheet.getLastRow(); //Returns the position of the last row that has content. 
    var projectNameRange = sheet.getRange(lastRow, 3); //Returns the range with the top left cell at the given coordinates. 
    var projectName = projectNameRange.getValues(); //Returns the value of the top-left cell in the range. 
    var sh = SpreadsheetApp.getActiveSheet() 
    var startcell = sh.getRange('E2').getValue(); 
    var colValues = sh.getRange('E2:E').getValues(); // get all the values in column A in an array 
    var max = 0; // define the max variable to a minimal value 
    for (var r in colValues) { // iterate the array 
     var vv = colValues[r][0].toString().replace(/[^0-9]/g, ''); // remove the letters from the string to convert to number 
     if (Number(vv) > max) { 
      max = vv 
     }; // get the highest numeric value in th column, no matter what happens in the column... this runs at array level so it is very fast 
    } 
    max++; // increment to be 1 above max value 
    sh.getRange(sh.getLastRow(), 5).setValue(Utilities.formatString('CG%06d', max)); // and write it back to sheet's last row. 
    var projectIdRange = sheet.getRange(lastRow, 5); 
    var projectId = projectIdRange.getValues(); //Returns the value of the top-left cell in the range. 

    var clientNameRange = sheet.getRange(lastRow, 3); 
    var clientName = clientNameRange.getValues(); 
    var curDate = Utilities.formatDate(new Date(), "GMT+1", "yyyy") 
    var productionSourceFolder = "TestTempFolder"; 
    var targetFolder = "target"; 
    var source = DriveApp.getFoldersByName(productionSourceFolder); 
    var target = DriveApp.createFolder(projectId + "_" + projectName + "_" + clientName + "_" + curDate); 

    if (source.hasNext()) { 
     copyFolder(source.next(), target); 
    } 

    DriveApp.getFolderById('0BwrizIzPM38bUGFPV2E1Q0JSMms').addFolder(target); 
    DriveApp.removeFolder(target); 
} 

function copyFolder(source, target) { 
    var folders = source.getFolders(); 
    var files = source.getFiles(); 

    while (files.hasNext()) { 
     var file = files.next(); 
     file.makeCopy(file.getName(), target); 
    } 

    while (folders.hasNext()) { 
     var subFolder = folders.next(); 
     var folderName = subFolder.getName(); 
     var targetFolder = target.createFolder(folderName); 
     copyFolder(subFolder, targetFolder); 
    } 

} 

function creativeCopy() { 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); //Returns the currently active spreadsheet, or null if there is none. 
    var sheet = ss.getActiveSheet(); //Gets the active sheet in a spreadsheet. 
    var lastRow = sheet.getLastRow(); //Returns the position of the last row that has content. 
    var projectNameRange = sheet.getRange(lastRow, 3); //Returns the range with the top left cell at the given coordinates. 
    var projectName = projectNameRange.getValues(); //Returns the value of the top-left cell in the range. 
    var sh = SpreadsheetApp.getActiveSheet() 
    var startcell = sh.getRange('E2').getValue(); 
    var colValues = sh.getRange('E2:E').getValues(); // get all the values in column A in an array 
    var max = 0; // define the max variable to a minimal value 
    for (var r in colValues) { // iterate the array 
     var vv = colValues[r][0].toString().replace(/[^0-9]/g, ''); // remove the letters from the string to convert to number 
     if (Number(vv) > max) { 
      max = vv 
     }; // get the highest numeric value in th column, no matter what happens in the column... this runs at array level so it is very fast 
    } 
    max++; // increment to be 1 above max value 
    sh.getRange(sh.getLastRow(), 5).setValue(Utilities.formatString('CG%06d', max)); // and write it back to sheet's last row. 
    var projectIdRange = sheet.getRange(lastRow, 5); 
    var projectId = projectIdRange.getValues(); //Returns the value of the top-left cell in the range. 
    var clientNameRange = clientNameRange.getRange(lastRow, 3); 
    var clientName = clientNameRange.getValues(); 
    var curDate = Utilities.formatDate(new Date(), "GMT+1", "yyyy") 
    var creativeSourceFolder = "TestTempFolder2"; 
    var targetFolder = "target"; 
    var source = DriveApp.getFoldersByName(creativeSourceFolder); 
    var target = DriveApp.createFolder(projectId + "_" + projectName + "_" + clientName + "_" + curDate); 

    if (source.hasNext()) { 
     copyFolder(source.next(), target); 
    } 

    DriveApp.getFolderById('0BwrizIzPM38bUGFPV2E1Q0JSMms').addFolder(target); 
    DriveApp.removeFolder(target); 

    var folders = source.getFolders(); 
    var files = source.getFiles(); 

    while (files.hasNext()) { 
     var file = files.next(); 
     file.makeCopy(file.getName(), target); 
    } 

    while (folders.hasNext()) { 
     var subFolder = folders.next(); 
     var folderName = subFolder.getName(); 
     var targetFolder = target.createFolder(folderName); 
     copyFolder(subFolder, targetFolder); 
    } 
} 
+0

謝謝阿德林。那就是訣竅。但是,我沒有收到新的錯誤:TypeError:在對象FolderIterator中找不到函數getFolders。 (第108行,文件「代碼」)。我現在不清楚這裏發生了什麼。 – Thunderbog

+0

如果您不介意,我會在明天上午(格林威治標準時間+2)09:00進行調查。 – Adelin

+1

我明白了。我剛剛刪除:'功能copyFolder(源,目標){'不知何故。我剛添加它,它的工作原理。再次感謝Adelin。 – Thunderbog

相關問題