简体   繁体   English

将工作表复制到新电子表格,将单元格值添加到新电子表格名称的末尾

[英]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.

相关问题 创建新的电子表格,以表格 Google Scripts 的名称命名 - Create new spreadsheet name it by the name of sheet Google Scripts 设置新单元格值时保留格式(Google 电子表格) - Preserve Formatting When Setting New Cell Value (Google Spreadsheet) Sheet Set value 制作电子表格的副本 - Sheet Set value the make copy of spreadsheet 我可以将数据从一个电子表格复制到新的电子表格吗? - Can I copy data from one spreadsheet to a new spreadsheet? Google脚本-将动态范围复制到新的电子表格 - Google Script - Copy dynamic range to new spreadsheet 按名称打开电子表格;如果找不到,则创建新的电子表格 - Open spreadsheet by name or create new spreadsheet if none is found 将 if 语句脚本添加到电子表格的新行 - Add if statement script to new rows of spreadsheet 为什么在向电子表格添加公式后,我的工作表没有添加新的数据行? - Why doesn't my sheet add new rows of data, after adding formulas to the spreadsheet? 将模板电子表格复制到文件夹,然后写入新副本(Google脚本) - Copy template spreadsheet to folder, then write to the new copy (google script) 如何制作电子表格复印机的循环(将整个电子表格复制到具有新名称的新电子表格中) - How to make a loop of an Spreadsheet copier (Copies an entire Spreadsheet into a new one with a new name)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM