Google应用脚本:如何制作电子表格副本并将其保存到特定文件夹? [英] Google App Script: How do I make copy of spreadsheet and save it to particular folder?

查看:226
本文介绍了Google应用脚本:如何制作电子表格副本并将其保存到特定文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google App Script。我试图:(1)制作电子表格的副本,(2)给副本一个特定的文件名,(3)将其保存到一个特定的文件夹。

I'm working with Google App Script. I am trying to: (1) make a copy of a spreadsheet, (2) give the copy a particular file name, and (3) save it to a particular folder.

在我看来,下面的代码应该可以工作,但事实并非如此。它确实完成任务1和3 - 也就是说,它会复制并将副本保存到我所需的文件夹中。但是,它不会将文件保存为我指定的文件名。相反,它将它保存为原始电子表格文件名的副本。

It seems to me that the code below should work, but it doesn't. It does accomplish tasks 1 and 3 - that is, it makes a copy and saves the copy to my desired folder. However, it does not save the file with the file name I specify. Instead, it saves it as "Copy of [original spreadsheet filename]."

(注意:我知道这个问题已经回答过了,但我相信它还没有)

(Note: I know this question has been answered before, but I believe it has not been answered since Google sunsetted DocList.)

谢谢!

Thanks!

  function saveAsSpreadsheet(){

    var sheet = SpreadsheetApp.getActiveSpreadsheet();  
    var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxx"); 

    DriveApp.getFileById(sheet.getId()).makeCopy("desired file name", destFolder);

   } //END function saveAsSpreadsheet


推荐答案

以下是为我工作的函数的已更正的代码。您没有为我使用方法 setNamedRange 设置命名范围并为其命名。并将变量名称更改为 TestRange 。此外, getValue()只会得到一个值,而您需要一个值范围,因此使用 getValues()代替。但是其余的代码都很好,并且工作得很好。

Here is the corrected code for your function that worked for me. You were not setting the named range for which I used the method setNamedRange and gave it a name. That and changing the variable name to which you were getting the range values to TestRange. Also, getValue() would only get you one value whereas you needed a range of values hence, used getValues() instead. But the rest of the code was fine and worked perfectly.

function saveAsSpreadsheet(){ 
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var range = sheet.getRange('Sheet1!A1:B3');
  sheet.setNamedRange('buildingNameAddress', range);
  var TestRange = sheet.getRangeByName('buildingNameAddress').getValues(); 
  Logger.log(TestRange); 
  var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxx"); 
  DriveApp.getFileById(sheet.getId()).makeCopy("Test File", destFolder); 
}

这篇关于Google应用脚本:如何制作电子表格副本并将其保存到特定文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆