如何制作电子表格的副本并将其保存到特定文件夹? [英] How do I make copy of spreadsheet and save it to particular folder?
问题描述
我正在使用 Google 应用脚本.我正在尝试:
I'm working with Google App Script. I am trying to:
- 制作电子表格的副本
- 为副本指定一个特定的文件名,然后
- 将其保存到特定文件夹.
在我看来,下面的代码应该可以工作,但事实并非如此.它确实完成了任务 1 和 3 - 也就是说,它制作了一个副本并将该副本保存到我想要的文件夹中.但是,它不会使用我指定的文件名保存文件.相反,它将其保存为[原始电子表格文件名]的副本".为此,我使用 File#复制
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]." To do this, I am using File#makeCopy
(注意:我知道这个问题之前已经回答过了,但我相信自从 Google 停止了 DocList 服务之后就没有回答过.)
(Note: I know this question has been answered before, but I believe it has not been answered since Google sunset the DocList service.)
当我制作这个简化的 MCVE 时,我发现它按预期工作:
When I made this simplified MCVE, I found that it works as expected:
function saveAsSpreadsheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxx");
DriveApp.getFileById(sheet.getId()).makeCopy("desired file name", destFolder);
} //END function saveAsSpreadsheet
但是,我的原始代码没有 - 副本的名称是错误的:
However, my original code does not - the copy's name is wrong:
function saveAsSpreadsheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var buildingNameAddress = sheet.getRangeByName('buildingNameAddress').getValue();
Logger.log(buildingNameAddress); //Logs correct string
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxxx");
DriveApp.getFileById(sheet.getId()).makeCopy(buildingNameAddress, destFolder);
}
推荐答案
这是对我有用的函数的更正代码.您没有设置我使用 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);
}
这篇关于如何制作电子表格的副本并将其保存到特定文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!