如何获取电子表格列的值

时间:2020-05-29 14:23:24

标签: javascript google-apps-script multidimensional-array google-sheets

我是app脚本和javascript的新手,但我设法使所有定义的值号或列号正确,除了右边的200列左右。但是列号并不总是出现,因为它有时会跳过一些空列,但由于某些原因却不会跳过其他列。有没有一种方法可以用脚本为列编号?我发现它可以成功运行,但是我不知道如何打印结果。

Sub Logo()
    Dim printWorksheet As Worksheet
    Dim logoShape As Shape
    Dim tempImageFile As String

    Set printWorksheet = ThisWorkbook.ActiveSheet
    Set logoShape = ThisWorkbook.Sheets("A").Shapes("myLogo")
    logoShape.Visible = True
    tempImageFile = Environ("temp") & Application.PathSeparator & "image.jpg"
    Call ShapeExportAsPicture(logoShape, tempImageFile)

    With printWorksheet.PageSetup
        .RightFooterPicture.Filename = tempImageFile
        .RightFooter = "&G"
    End With
    logoShape.Visible = False
End Sub

Private Sub ShapeExportAsPicture(pShape As Shape, sPathImageLocation As String)
    Dim sTempChart As String
    Dim shTempSheet As Worksheet
    Set shTempSheet = pShape.Parent
    Charts.Add 'Add a temporary chart
    ActiveChart.Location Where:=xlLocationAsObject, Name:=shTempSheet.Name
    Selection.Border.LineStyle = 0
    sTempChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)
    With shTempSheet
        'Change the dimensions of the chart to the size of the original shape
        With .Shapes(sTempChart)
            .Width = pShape.Width
            .Height = pShape.Height
        End With
        pShape.Copy  'Copy the shape
        With ActiveChart 'Paste the shape into the chart
            .ChartArea.Select
            .Paste
        End With
        'export the chart
        .ChartObjects(1).Chart.Export Filename:=sPathImageLocation, FilterName:="jpg"
        .Shapes(sTempChart).Delete 'Delete the chart.
      End With
End Sub

1 个答案:

答案 0 :(得分:0)

function numberColumns() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  const rg=sh.getRange(1,1,1,sh.getMaxColumns());
  var v=rg.getValues()[0].map(function(e,i){return i+1;});
  sh.insertRowBefore(1);//inserts a row first to hold column numbers
  rg.setValues([v]);
}
相关问题