需要工作表脚本以将img保存到驱动器 [英] Need sheets script to save img to drive
问题描述
我需要一个脚本来与Google表格一起使用,以将.img网址列表保存到特定的Google驱动器文件夹中,并从另一个单元格中命名文件.
I need a script to use with google sheets to save a list of .img urls to specific google drive folder with file naming from another cell.
例如:
列A-文件网址
/imagefilepath.jpg
B列-另存为名称
图片_1
For Example:
Column A - File URL
/imagefilepath.jpg
Column B - Save as name
image_1
将/imagefilepath.jpg自动保存到带有"image_1"文件名的Google驱动器文件夹测试"中.
Auto save /imagefilepath.jpg to google drive folder "Test" with "image_1" file name.
有可能吗?
脚本的新手..需要一些建议/指针或工作脚本(甚至更好)!
New to scripts.. need some advice/pointers or working script (even better)!
当前获得以下信息:
/**
* Uploads a new file to the user's Drive.
*/
function uploadFile() {
var imgurl = 'https://www.w3schools.com/w3css/img_lights.jpg';
var image = UrlFetchApp.fetch(imgurl).getBlob();
var filename = setName('test');
var folder = DriveApp.getFoldersByName(folder);
if (folder != null) {
var file = DriveApp.createFile(image);
}
Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}
但是它仅以URL名称的形式保存到Google驱动器的根目录中,并且仅当我将img URL插入脚本时才保存.如何使其具有动态性?
But it saves as url name into the root of google drive and only if I insert the img urls into the script. How do i make it dynamic?
P.s在这里找到了一些脚本,但是所有脚本似乎都已过时/无法使用.我有2000多个网址和不同的文件名.
P.s found a few scripts on here but all seem to be outdated/dont work. I have over 2000 urls and different file names.
推荐答案
尝试一下:
function uploadFile() {
var imgurl = 'https://www.w3schools.com/w3css/img_lights.jpg';
var image = UrlFetchApp.fetch(imgurl).getBlob().getAs('image/jpeg').setName('test');
var folder = DriveApp.getFolderById('FolderId');
var file = DriveApp.createFile(image);
Drive.Files.update({"parents": [{"id": folder.getId()}]}, file.getId());
}
因此,您的最终功能应如下所示:
So your final function should look something like this:
function uploadFiles() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var vA=rg.getValues();
for(var i=0;i<vA.length;i++) {
var imgurl=vA[i][0];//Column A for url
var name=vA[i][1];//Column B for filename
var image=UrlFetchApp.fetch(imgurl).getBlob().getAs('image/jpeg').setName(name);
var folder=DriveApp.getFolderById('1aIawMeJCjB1GWaV6URH3jkV4xUJhaYLV');
var file=DriveApp.createFile(image);
Drive.Files.update({"parents": [{"id": folder.getId()}]}, file.getId());
}
}
这篇关于需要工作表脚本以将img保存到驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!