简体   繁体   English

将数据从一个电子表格文件复制到另一个

[英]Copy data from one spreadsheet file to another

I have a Google spreadsheet that I use to track my open crypto trades.我有一个 Google 电子表格,用于跟踪我的公开加密交易。 The information consists of 4 columns.该信息由 4 列组成。 ("Coin", "Date", "Time" and "Price"). (“硬币”、“日期”、“时间”和“价格”)。 This information is not available to the public.此信息不向公众公开。 When trades are closed I complete the next 3 columns ("Date", "Time" and "Price") and copy all 7 columns across to a different spreadsheet that is available to the public.当交易结束时,我完成接下来的 3 列(“日期”、“时间”和“价格”)并将所有 7 列复制到另一个可供公众使用的电子表格。 There are other columns in this public spreadsheet that contain formulae that calculate trade profit and loss, weekly summary and monthly summary.此公开电子表格中还有其他列包含计算交易损益、每周摘要和每月摘要的公式。

I have written the following code in Google Apps Script and attached it to a button on one of the spreadsheets to automate this task.我在 Google Apps 脚本中编写了以下代码并将其附加到其中一个电子表格上的按钮以自动执行此任务。 I need all users to be able to run the code by clicking the button.我需要所有用户都能够通过单击按钮来运行代码。 Currently there are 4 users.目前有4个用户。 My code does all of this automatically except, because I open a new spreadsheet part way through the script, none of the values collected before that point are available to be inserted into the target spreadsheet.Any help would be greatly appreciated.我的代码会自动执行所有这些操作,除了因为我在脚本中打开了一个新的电子表格,所以在该点之前收集的所有值都无法插入到目标电子表格中。任何帮助将不胜感激。

function updatePublicSheet(){
  var coinBought;
  var dateBought;
  var timeBought;
  var priceBought;
  var dateSold;
  var timeSold;
  var priceSold;
  var lrIndex;
  var sss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1cBGpD0nUqCGtQqq78svw4fxA4ZgwBqV2yzIbgY1tY2o/edit#gid=1107419239"); 
  var sSheet = sss.getSheetByName('SS Open Trades');
  var sRng = sSheet.getRange("F3:H").getValues();

  // Find last entry in columns F, G or H in the Open Trades sheet
  for (var i = sRng.length-1;i>=0;i--){
    var lrIndex = i;
      if (!sRng[i].every(function(c){return c == "";})){
        break;
      }
    }
    //  Logger.log("Last row is %s", lrIndex + 3);

    // Check that the closed information has been added to all columns
    // index 6 = 6th column = F = dateSold
    // index 7 = 7th column = G = timeSold
    // index 8 = 8th column = H = priceSold

    if ((sSheet.getRange((lrIndex + 3),6).getValue().length) == 0 ||
      (sSheet.getRange((lrIndex + 3),7).getValue().length) == 0 ||
      (sSheet.getRange((lrIndex + 3),8).getValue().length) == 0){
      //Logger.log("Trade close information is not complete");
        return;
        }
  
  // If there is a complete closed entry, copy the values to the variables
  if (lrIndex > 0)
    {
      coinBought = sSheet.getRange((lrIndex + 3),2).getValue();
      dateBought = sSheet.getRange((lrIndex + 3),3).getValue();
      timeBought = sSheet.getRange((lrIndex + 3),4).getValue();
      priceBought = sSheet.getRange((lrIndex + 3),5).getValue();
      dateSold = sSheet.getRange((lrIndex + 3),6).getValue();
      timeSold = sSheet.getRange((lrIndex + 3),7).getValue();
      priceSold = sSheet.getRange((lrIndex + 3),8).getValue();
      //Logger.log(coinBought);
    }

  var tss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1umkTCr95FZUrZzv0e_ZDD9QZYiiYuH1fJohPYQzNE9Q/edit#gid=1644116137"); 
  var tSheet = tss.getSheetByName('Trade Tracker');
  var tRng = tSheet.getRange("A3:H").getValues();

  // Create a new row at Row 4 on the Trades Tracker Sheet
  tSheet.insertRows(5, 1);//shift all rows down by one from row 5

  // Copy values from row 4 to row 5, including the formulae
  var tRange = tSheet.getRange(4, 1, 1, 26);
  tRange.copyTo(tSheet.getRange(5, 1, 1, 26), {contentsOnly:false});

  // Populate row 4, first 7 table columns, from the variables
  //Logger.log(coinBought);
  tSheet.getRange(4,2).setValue(coinBought);
  tSheet.getRange(4,3).setValue(dateBought);
  tSheet.getRange(4,4).setValue(timeBought);
  tSheet.getRange(4,5).setValue(priceBought);
  tSheet.getRange(4,6).setValue(dateSold);
  tSheet.getRange(4,7).setValue(timeSold);
  tSheet.getRange(4,8).setValue(priceSold);

  // Sort the sheet by date/time closed
    // Find last entry in Trade Tracker sheet
  for (var i = tRng.length-1;i>=0;i--){
    var lrIndex = i;
      if (!tRng[i].every(function(c){return c == "";})){
        break;
      }
    }
    Logger.log("Last row is %s", lrIndex + 4);

  var tRange = tSheet.getRange(4, 2, lrIndex + 4, 11)
  tRange.sort([{column: 6, ascending: false}, {column: 7, ascending: false}]);

  // On the source sheet, delete the row just copied and add another blank row

  // Success message

}

Issue:问题:

The script should run under an account that has access to the source spreadsheet.该脚本应在有权访问源电子表格的帐户下运行。 Therefore, executing this via button will not work, since the function will always execute under the user who clicked the button, and that, as I understood, might not have access to the non-public spreadsheet.因此,通过按钮执行此操作将不起作用,因为 function 将始终在单击该按钮的用户下执行,并且据我所知,可能无法访问非公开电子表格。

Solution:解决方案:

Therefore, you should execute it a different way.因此,您应该以不同的方式执行它。 I'd suggest the following:我建议如下:

  • With an account that has access to the source spreadsheet , install an onEdit trigger, either manually, by following these steps , or programmatically , by copying this function to your editor and executing it once:使用有权访问源电子表格的帐户,按照以下步骤手动或以编程方式安装onEdit触发器,方法是将此 function 复制到您的编辑器并执行一次:
function installOnEdit() {
  var sss SpreadsheetApp.openByUrl("TARGET_SPREADSHEET_URL");
  ScriptApp.newTrigger("updatePublicSheet")
    .forSpreadsheet(sss)
    .onEdit()
    .create();
}
  • Add a checkbox to a cell in the target spreadsheet.将复选框添加到目标电子表格中的单元格。
  • Modify your function so that it only updates a spreadsheet if the checkbox cell is edited.修改您的 function,使其仅在编辑复选框单元格时更新电子表格。 You can use the event object for this:您可以为此使用事件 object
function updatePublicSheet(){
  var range = e.range;
  var sheet = range.getSheet();
  var sheetName = "CHECKBOX_SHEET"; // Name of the sheet where the checkbox is found, change accordingly
  var checkboxNotation = "A1"; // Notation of the checkbox cell, change accordingly
  if (sheet.getName() === sheetName && range.getA1Notation() === checkboxNotation) {
    // YOUR OLD CODE
    range.uncheck();
  }
}

Once you do that, every time the checkbox is clicked by anyone, the taget spreadsheet is updated.一旦你这样做了,每次任何人点击复选框时,taget 电子表格都会更新。

Note:笔记:

  • Using getValue and setValue to get or set values to multiple cells is not recommended.不建议使用getValuesetValue获取或设置多个单元格的值。 You can use getValues and setValues to grab and modify many cell values at once, minimizing the amount of calls to the Sheets service you make.您可以使用getValuessetValues一次获取和修改许多单元格值,从而最大限度地减少对表格服务的调用次数。 See Best practices .请参阅最佳实践

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

相关问题 在编辑时将数据从一个电子表格复制到另一个电子表格 - Copy data from one spreadsheet to another on edit 将过滤器数据从一个电子表格文件复制到另一个 - Google Apps 脚本 - Copy filter data from one spreadsheet file to another - Google Apps Script 将数据从一张纸复制到另一张电子表格,但不包括特定的列 - Copy data from one sheet to another spreadsheet exclude specific columns 是否可以将数据范围从一个电子表格复制到另一个电子表格? - Is possible to copy a data range from one spreadsheet to another? 如何在编辑时将数据从一个电子表格复制到另一个电子表格 - How to copy data from one spreadsheet to another on edit 将数据从一个电子表格复制到另一种保留格式且不使用公式 - Copy data from one Spreadsheet to another keeping format and without formulas 将范围从一个电子表格复制到另一个 - Copy a range from one spreadsheet to another 将多个工作表从一个电子表格复制到另一个电子表格 - Copy several sheets from one spreadsheet to another spreadsheet 将动态范围从一个电子表格复制到另一个使用getfileid调用的电子表格 - copy dynamic range from one spreadsheet to another spreadsheet called with getfileid 如何将脚本从一个电子表格复制到另一电子表格? - How do I copy a script from one spreadsheet to another spreadsheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM