如何在textarea中复制多行和多行文本并粘贴到工作表?

时间:2018-05-27 03:18:29

标签: google-apps-script textarea copy-paste multiline multirow

我正在尝试将文本粘贴到textarea中并将其粘贴到工作表中,例如Ctrl + shift + V命令。以下脚本只粘贴在一个单元格中,但我想粘贴工作表中的跨越方式(如在Ctrl + shift + v中复制和粘贴)。

这是html示例文件: teste.html

<!DOCTYPE html>
<html>
  <!-- Document Head -->
  <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <base target="_top">
    <!-- Add the Google Apps Script CSS file -->
    <link rel="stylesheet"     href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">

  <link rel="stylesheet"     href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js">    </script>

  <script>
  $( function() {
    $( "#tabs" ).tabs();
  } );
  </script>

<!-- Add Styling to your sidebar -->    
<!-- You can also refer to an external stylesheet - as with the link above or other css frameworks like Bootstrap or W3School's CSS -->
<!-- Try not to have styling elements within your html page and rather make of use external stylesheets -->
  <style>
    body {
      padding-left: 10px;
    }
    a:active {
      color: white;
      text-decoration: none:
   }
   a:hover {
      color: white;
      text-decoration: none:
   }
   a:link {
      color: white;
      text-decoration: none:
   }
   a:visited {
      color: white;
      text-decoration: none:
   }
   div {
      padding: 3px;
   }
    </style>
  </head>

  <!-- Document Body -->
  <body>

  <h2>Colar dados do GDL</h2>

<form id="myform">
  <div>
  <textarea  rows="150" cols="10" id = "textareagdl"     style="width:200px;height:150px;"></textarea>  
  </div>
  <div>
<button type="button"onclick="myFunction()">Copy</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("textareagdl").value;
    google.script.run.colargdl(x);
}
</script>

  </div>
</form>

</body>
</html>

以下是google脚本文件: codigo.gs

function onOpen() {
  showSidebar();
}

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('teste')
      .setTitle('Create')
      .setWidth(300);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showSidebar(html);
}

function colargdl(dadoscsv){
var ss = SpreadsheetApp.getActive();
ss.getSheetByName('teste').getRange('A1:J120').clear();

var sheet = ss.getSheetByName('teste').getRange('A1').setValue(dadoscsv);

//dadoscsv.copyTo(sheet, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
//values.copyTo(sheet.getRange("A1"), {contentsOnly:true});
}

此致

1 个答案:

答案 0 :(得分:0)

在Google Apps脚本中,您可以调用range.setValues()一次填写多个单元格。 setValues采用二维数组值。您必须将文本区域的内容转换为二维数组。完成后,您可以拨打range.setValues()

您需要获得与数组维度相匹配的范围 - sheet.getRange(row, col, numRows, numCols)执行此操作:

var data = [["one", "two", "three"], ["four", "five", "six"]];
var range = sheet.getRange(1, 1, data.length, data[0].length);
range.setValues(data);