[英]Copy sheet to new spreadsheet, Add cell value to end of new spreadsheet name
This is now creating the copy and adding the text to the end of the file name. 现在,这将创建副本并将文本添加到文件名的末尾。 The new file is located in the same directory as the original file.
新文件与原始文件位于同一目录中。 I can't seem to move the new file to the "Timesheet Archive" folder.
我似乎无法将新文件移到“时间表存档”文件夹中。 Seems like I should just be able to specify the location for the new file?
似乎我应该能够指定新文件的位置?
var range = sheet.getRange(2,7);
var data = range.getValue();
var SSID = '1S3Vj5lYOchh0OJ5ZCamsoMCKtcTHeFd72nyxPHJYofk'
var CopyDate = Utilities.formatDate(data , "GMT-8", "yy_MM_dd");
var folder = DriveApp.getFoldersByName('Timesheet Archive');
var id = DriveApp.getFileById(SSID)
id.makeCopy(SpreadsheetApp.openById(SSID).getName() + "_" + CopyDate);
I am moving to Google's new Sheets and am having trouble with this function which was working fine prior to the move: 我要使用Google的新版表格,并且在使用此功能之前遇到了麻烦,该功能在使用之前可以正常工作:
function exportData() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Timesheet");
var sourceData = sourceSheet.getDataRange().getValues();
sourceData.splice(0,1); // Remove header
var targetSS = SpreadsheetApp.openById("1ZKQw9WnAFmJfC6GP_CYmSbQK_820zIR6oQGnLMc1yBM").getSheetByName("Master Time Sheet");
var targetRangeTop = targetSS.getLastRow(); // Get # rows currently in target
targetSS.getRange(targetRangeTop+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
var sheet = SpreadsheetApp.getActive().getSheetByName('Timesheet');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var name = ss.getName(); // Not necessary
var sheet = ss.getSheetByName('Timesheet'); // or whatever is the name of the sheet
var range = sheet.getRange(2,7);
var data = range.getValue();
var SSID = '1S3Vj5lYOchh0OJ5ZCamsoMCKtcTHeFd72nyxPHJYofk'
var CopyDate = Utilities.formatDate(data , "GMT-8", "yy_MM_dd"); // Function Date + Format
var folder = DocsList.getFolder('Timesheet Archive'); //Use this line if you want to get the folder by name
var backup = DocsList.getFileById(SSID).makeCopy(SpreadsheetApp.openById(SSID).getName() + "_" + CopyDate);
backup.addToFolder(folder); //This line will move the file
backup.removeFromFolder(DocsList.getRootFolder()); //This line is needed to remove the File from the Root
}
The first part copies part of the sheet to another existing spreadsheet and this part of the function is working properly. 第一部分将工作表的一部分复制到另一个现有的电子表格中,该功能的这一部分正常运行。 The second part copies the entire sheet, creates a new file using this copied sheets name and data and appends the end of the file name using text (a date) from a cell located on the source sheet.
第二部分复制整个工作表,使用该复制的工作表名称和数据创建一个新文件,并使用来自源工作表上单元格的文本(日期)附加文件名的末尾。 It is supposed to first move it to the root folder and then to another folder titled "Timesheet Archive".
应该首先将其移动到根文件夹,然后再移动到另一个名为“时间表存档”的文件夹。
I am not getting any errors and the file is not appearing in either the root folder nor in the "Timesheet Archive" folder. 我没有收到任何错误,该文件也没有出现在根文件夹或“时间表存档”文件夹中。
You are using: 您正在使用:
DriveApp.getFoldersByName('Timesheet Archive');
That gets a "Folder Iterator" 那得到一个“文件夹迭代器”
You need to either use: 您需要使用以下任一方法:
getFolderById(id)
Or iterate through the folders, (Even though there may only be one!) 或遍历文件夹,(即使可能只有一个!)
while (folders.hasNext()) {
var folder = folders.next();
if (folder === 'Timesheet Archive') {
//Make copy here
};
}
There is no addToFolder()
method of the File Class. 没有File类的
addToFolder()
方法。 backup
is a file. backup
是一个文件。 The makeCopy()
method has a destination
option. makeCopy()
方法具有destination
选项。 You can create the backup and put it into the destination all at the same time. 您可以同时创建备份并将其全部放入目标中。
https://developers.google.com/apps-script/reference/drive/file#makeCopy(String,Folder) https://developers.google.com/apps-script/reference/drive/file#makeCopy(String,Folder)
Use your folder
variable, that is a folder type as the destination. 使用
folder
变量,即文件夹类型作为目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.