简体   繁体   English

代码通过它的名称获取电子表格URL并粘贴到另一个电子表格中

[英]Code to get Spreadsheet URL through it's name and paste in another spreadsheet

So, I have a sheet with 2 columns, column A is the name of the spreadsheet and column B is it's URL. 所以,我有一个包含2列的工作表,A列是电子表格的名称,B列是它的URL。 So I'm using a script to get the url, through it's name, but the last line isn't working. 所以我使用脚本来获取URL,通过它的名称,但最后一行不起作用。 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. while循环中的return url语句在getValue运行之前退出函数。

I suspect what you want is to replace the return url statement with the getValue statement so it runs for each row. 我怀疑你想要的是用getValue语句替换return url语句,以便它为每一行运行。

while (files.hasNext()){
   var file = files.next();
   var url = file.getUrl();
   getValue(url).Paste_Values(getRange(newRow, 2), {contentsOnly: true});
}

Getting Spreadsheet URLs 获取电子表格URL

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. 您将使用代码遇到的一个问题是,当您添加更多网址时,newrow不会更改,实际上您实际上希望文件名旁边的网址不在列表的末尾。 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. 此函数将vA1 [i] [0]中的文件名放入url列vA2 [i] [0]中,并且还计算每个名称的文件数并记录信息并将其显示在对话框中在功能结束时,让您有机会确保您已获得正确的文件。 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")
  }
}

Making changes via a Dailog 通过Dailog进行更改

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM