如何制作电子表格的副本并将其保存到特定的文件夹? [英] How do I make copy of spreadsheet and save it to particular folder?
问题描述
我正在使用Google App脚本.我正在尝试:
I'm working with Google App Script. I am trying to:
- 复制电子表格
- 为副本提供特定的文件名,然后
- 将其保存到特定文件夹.
在我看来,下面的代码应该可以,但是不能.它完成,完成任务1和3-即制作副本并将副本保存到我所需的文件夹中.但是,它不会使用我指定的文件名保存文件.而是将其保存为"[原始电子表格文件名]的副本".为此,我正在使用 File#makeCopy
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屋!