So, I have a sheet with 2 columns, column A is the name of the spreadsheet and column B is it's URL. So I'm using a script to get the url, through it's name, but the last line isn't working. Can anyone help me?
function spreadsheetUrl() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName('Page1'));
var lastRow = ss.getRange("B2:B200").getLastRow();
var newRow = lastRow+1;
var range = SpreadsheetApp.getActiveSheet().getRange(newRow, 1);
var spreadsheetName = range.getValue();
var files = DriveApp.getFilesByName(spreadsheetName);
while (files.hasNext()){
var file = files.next();
var url = file.getUrl();
return url
}
getValue(url).Paste_Values(getRange(newRow, 2), {contentsOnly: true});
}
The return url
statement in the while loop exits the function before getValue
runs.
I suspect what you want is to replace the return url
statement with the getValue
statement so it runs for each row.
while (files.hasNext()){
var file = files.next();
var url = file.getUrl();
getValue(url).Paste_Values(getRange(newRow, 2), {contentsOnly: true});
}
One of the problems you're going to have with your code is that newrow is not changing as you add more urls and infact you actually want the url next to the file name not at the end of the list. This function puts the files names which are in vA1[i][0] into the urls column which is vA2[i][0] and it also counts the number of files with each name and records the information and displays it in a dialog at the end of the function thus giving you the opportunity to insure that you have gotten the correct files. This occurs a lot if your in the habit on storing backups of you files.
One could also consider putting the dialog within the loop and thus letting you choose which one you wish to save but that would require you to be watching it while the function is running.
function spreadsheetUrl() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Page1');
var rg1=sh.getRange(2,1,sh.getLastRow(),1);
var rg2=sh.getRange(2,2,sh.getLastRow(),1);
var vA1=rg1.getValues();//Spreadsheet Names
var vA2=rg2.getValues();//Urls
var xf=[];
for(var i=0;i<vA1.length;i++) {
if(vA1[i][0]) {
var files=DriveApp.getFilesByName(vA1[i][0]);
var n=0;
while (files.hasNext()){
var file = files.next();
if(n==0) {
vA2[i][0]=file.getUrl();
}else{
xf.push({name:file.getName(),url:file.getUrl(),id:file.getId()});
}
n++;
}
}
}
rg2.setValues(vA2);
var html='<h1>You have additional files with the same names</h1>';
if(xf.length) {
for(var i=0;i<xf.length;i++) {
html+=Utilities.formatString('<br />Name: %s Url: %s Id: %s',xf[i].name, xf[i].url,xf[i].id);
}
html+='<br /><input type="button" value="Exit" onClick="google.script.host.close();" />';
var userInterface=HtmlService.createHtmlOutput(html).setWidth(1000);
SpreadsheetApp.getUi().showModelessDialog(userInterface, "Files with Identical Names")
}
}
I played around with this a bit and this version allows you make modifications to the urls posted when there are more that one file of a given name. It does so at the end rather than in the middle of the loop so you don't have to do several times while the loop is running. This version also has an accompanying function for writing changes to the sheet server side.
function spreadsheetUrl() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Page1');
var rg1=sh.getRange(2,1,sh.getLastRow(),1);
var rg2=sh.getRange(2,2,sh.getLastRow(),1);
var vA1=rg1.getValues();//Spreadsheet Names
var vA2=rg2.getValues();//Urls
//rg2.clearContent();//debug
var xf=[];
for(var i=0;i<vA1.length;i++) {
if(vA1[i][0]) {
var files=DriveApp.getFilesByName(vA1[i][0]);
var n=0;
while (files.hasNext()){
var file = files.next();
if(n==0) {
vA2[i][0]=file.getUrl();
}else{
xf.push({name:file.getName(),url:file.getUrl(),id:file.getId(),row:i+2});
}
n++;
}
}
}
rg2.setValues(vA2);
var html='<h1>You have additional files with the same names</h1>';
html+='<br />Change them if you wish by select the select Me button for the appropriate selection.';
html+='<br />Press close when you done.';
if(xf.length) {
for(var i=0;i<xf.length;i++) {
html+=Utilities.formatString('<br />Name: %s Id: %s<input type="button" value="Select Me" onClick="selectMe(\'%s\',%s);" />',xf[i].name,xf[i].id,xf[i].url,xf[i].row);
}
html+='<br /><input type="button" value="Exit" onClick="google.script.host.close();" />';
html+='<script>function selectMe(value,row){console.log(value);console.log(row);google.script.run.selectMe("Page1",value,row);}console.log("My Code");</script>';
var userInterface=HtmlService.createHtmlOutput(html).setWidth(400).setHeight(300);
SpreadsheetApp.getUi().showModelessDialog(userInterface, "Files with Identical Names")
}
}
function selectMe(sheetname,value,row) {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName(sheetname);
var rg=sh.getRange(row,2).setValue(value);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.