我遇到了一些麻烦。预先感谢您的任何帮助。我刚刚使用if语句更新了一个脚本,我需要为新表单中的响应添加该语句。我的目标是复制一个文件夹以及两个不同的" source"的子文件夹和文件。基于表单响应中的值的文件夹。因此,例如,如果响应是“生产”。然后复制" TestTempFolder"如果回复是' Creative'然后复制" TestTempFolder2"到已创建的新文件夹并移动到" Projects"存储所有项目的文件夹。我不确定我做错了什么。这是我更新的脚本:
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 :(得分: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);
}
}