You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将textarea中多行多列文本按Ctrl+Shift+V格式粘贴到Google表格?

实现Google表格侧边栏文本按行列批量粘贴

我明白你现在的困扰——你想把侧边栏textarea里的内容像按Ctrl+Shift+V那样自动拆分到表格的对应单元格,但当前脚本直接把所有内容塞进了单个单元格。咱们来一步步搞定这个问题。

问题根源

你的colargdl函数里用了setValue(dadoscsv),这个方法只会把整个字符串放到指定单元格里,完全没处理文本里的行列结构。要实现按行列分布,咱们得先把文本拆成二维数组,再用setValues批量写入表格。

解决方案

修改你的codigo.gs文件里的colargdl函数,核心是把文本拆成对应表格行列的二维数组,再批量写入。步骤如下:

  1. 把输入文本按换行符拆分成单独的行
  2. 每行再按列分隔符(比如制表符\t、空格或逗号,根据你的实际内容调整)拆分成单元格内容
  3. 过滤掉空行避免无效数据
  4. 把处理后的二维数组写入表格对应的范围

修改后的完整代码

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)

修改colargdl函数,这里假设你的文本每行的列是用制表符分隔的,如果是其他分隔符(比如逗号、空格),只需要调整split的参数即可:

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(); 
  var sheet = ss.getSheetByName('teste');
  // 先清空指定范围
  sheet.getRange('A1:J120').clear();
  
  // 处理文本:拆分成对应表格的二维数组
  var rows = dadoscsv.split('\n')
    .filter(row => row.trim() !== '') // 过滤空行,避免无效数据
    .map(row => row.split('\t')); // 按制表符拆分列,可根据实际内容调整分隔符
  
  // 如果有有效数据,批量写入表格
  if(rows.length > 0){
    // 动态确定写入范围:行数为拆分后的行数,列数为最长行的列数
    var numRows = rows.length;
    var numCols = Math.max(...rows.map(row => row.length));
    sheet.getRange(1, 1, numRows, numCols).setValues(rows);
  }
}

关键说明

  • split('\n'):把整个文本按换行拆成每行的字符串数组,对应表格的行
  • filter(row => row.trim() !== ''):去掉空行,避免表格里出现无用的空行
  • map(row => row.split('\t')):把每行拆成单元格内容的数组,最终得到的二维数组正好匹配表格的行列结构
  • setValues(rows):这个方法可以一次性把二维数组写入表格,效果和手动按Ctrl+Shift+V完全一致

如果你的文本列分隔符不是制表符,比如是逗号,就把split('\t')改成split(',');如果是多个空格分隔,用split(/\s+/)即可。

内容的提问来源于stack exchange,提问作者Sara Rodriguez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:08:27